Commit a47a56d8 authored by Andrei Mihu's avatar Andrei Mihu
Browse files

Better error responses from runtime RPC or HTTP hooks

parent bd39bf86
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -19,6 +19,7 @@ The format is based on [keep a changelog](http://keepachangelog.com/) and this p
- Increase default maximum length of various name, location, timezone, and other free text fields to 255 characters.
- Increase default maximum length of storage bucket, collection, and record from 70 to 128 characters.
- Increase default maximum length of topic room names from 64 to 128 characters.
- Better error responses when runtime function RPC or HTTP hooks fail or return errors.

### Fixed
- Realtime notification routing now correctly resolves connected users.
+17 −3
Original line number Diff line number Diff line
@@ -15,9 +15,9 @@
package server

import (
	"fmt"

	"github.com/yuin/gopher-lua"
	"go.uber.org/zap"
	"strings"
)

func (p *pipeline) rpc(logger *zap.Logger, session *session, envelope *Envelope) {
@@ -36,7 +36,21 @@ func (p *pipeline) rpc(logger *zap.Logger, session *session, envelope *Envelope)
	result, fnErr := p.runtime.InvokeFunctionRPC(lf, session.userID, session.handle.Load(), session.expiry, rpcMessage.Payload)
	if fnErr != nil {
		logger.Error("Runtime RPC function caused an error", zap.String("id", rpcMessage.Id), zap.Error(fnErr))
		session.Send(ErrorMessage(envelope.CollationId, RUNTIME_FUNCTION_EXCEPTION, fmt.Sprintf("Runtime function caused an error: %s", fnErr.Error())))
		if apiErr, ok := fnErr.(*lua.ApiError); ok {
			msg := apiErr.Object.String()
			if strings.HasPrefix(msg, lf.Proto.SourceName) {
				msg = msg[len(lf.Proto.SourceName):]
				msgParts := strings.SplitN(msg, ": ", 2)
				if len(msgParts) == 2 {
					msg = msgParts[1]
				} else {
					msg = msgParts[0]
				}
			}
			session.Send(ErrorMessage(envelope.CollationId, RUNTIME_FUNCTION_EXCEPTION, msg))
		} else {
			session.Send(ErrorMessage(envelope.CollationId, RUNTIME_FUNCTION_EXCEPTION, fnErr.Error()))
		}
		return
	}

+16 −1
Original line number Diff line number Diff line
@@ -38,6 +38,7 @@ import (
	"github.com/gorilla/mux"
	"github.com/gorilla/websocket"
	"github.com/satori/go.uuid"
	"github.com/yuin/gopher-lua"
	"go.uber.org/zap"
	"golang.org/x/crypto/bcrypt"
	"nakama/pkg/httputil"
@@ -230,7 +231,21 @@ func (a *authenticationService) configure() {
		responseData, funError := a.runtime.InvokeFunctionHTTP(fn, uuid.Nil, "", 0, payload)
		if funError != nil {
			a.logger.Error("Runtime function caused an error", zap.String("path", path), zap.Error(funError))
			http.Error(w, fmt.Sprintf("Runtime function caused an error: %s", funError.Error()), 500)
			if apiErr, ok := funError.(*lua.ApiError); ok {
				msg := apiErr.Object.String()
				if strings.HasPrefix(msg, fn.Proto.SourceName) {
					msg = msg[len(fn.Proto.SourceName):]
					msgParts := strings.SplitN(msg, ": ", 2)
					if len(msgParts) == 2 {
						msg = msgParts[1]
					} else {
						msg = msgParts[0]
					}
				}
				http.Error(w, msg, 500)
			} else {
				http.Error(w, funError.Error(), 500)
			}
			return
		}