Skip to content

Commit

Permalink
Better error responses from runtime RPC or HTTP hooks
Browse files Browse the repository at this point in the history
  • Loading branch information
zyro committed Sep 29, 2017
1 parent bd39bf8 commit a47a56d
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 4 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
20 changes: 17 additions & 3 deletions server/pipeline_runtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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
}

Expand Down
17 changes: 16 additions & 1 deletion server/session_auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
}

Expand Down

0 comments on commit a47a56d

Please sign in to comment.