Example #1
0
func (this *Processor) Invoke(conn *Connection, cmd *Command) (result interface{}, err error) {
	message := &Message{
		Conn:    conn,
		Command: cmd,
		Context: dynamic.Empty(),
	}
	defer func() {
		this.After(message, result, err)
	}()
	defer func() {
		if r := recover(); r != nil {
			log.Println(r)
			console.JSON(cmd)
			log.Println(string(debug.Stack()))
			var ok bool
			if err, ok = r.(error); !ok {
				err = Exception(fmt.Sprint(r))
			}
		}
	}()
	this.Before(message)
	handlers := this.handlers[cmd.Action]
	if handlers == nil {
		return nil, Error("No handlers for " + cmd.Action)
	}
	for _, cb := range handlers {
		result, err = cb(message)
		if err != nil {
			panic(err)
		}
	}
	return result, nil
}
Example #2
0
func (this *Connection) respond(key string, resp interface{}, err error) {
	cmd := &Command{
		Key: key,
	}
	if err == nil {
		cmd.Action = RESPONSE
		cmd.Body = resp
	} else {
		if _, ok := err.(*DRSError); ok {
			cmd.Action = ERROR
			cmd.Body = dynamic.Build(
				"message", err.Error(),
			)
		} else {
			console.JSON("EXCEPTION", err)
			cmd.Action = EXCEPTION
			cmd.Body = dynamic.Build(
				"message", err.Error(),
			)
		}
	}
	this.Stream.Encode(cmd)
}