Skip to content

Commit

Permalink
Improve js runtime custom error rpc response payload (#717)
Browse files Browse the repository at this point in the history
If 'message' field is set and non-empty in js runtime thrown error object, set it in the returned json payload response.

Resolves #689
  • Loading branch information
sesposito committed Nov 30, 2021
1 parent 419be0d commit f4490d9
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 19 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Expand Up @@ -13,6 +13,9 @@ The format is based on [keep a changelog](http://keepachangelog.com) and this pr
- Fix handling of leaderboard record writes that do not need to update the database.
- Fix parsing edge case in TypeScript/JavaScript runtime storage delete operations.

### Changed
- Set JS runtime custom error message as the returned payload message in RPC requests.

## [3.9.0] - 2021-10-29
### Added
- Allow creation of relayed matches with a name. Names will be mapped to match identifiers.
Expand Down
26 changes: 7 additions & 19 deletions server/runtime_javascript.go
Expand Up @@ -74,7 +74,6 @@ func (r *RuntimeJS) GetCallback(e RuntimeExecutionMode, key string) string {

type jsError struct {
StackTrace string `json:"stackTrace,omitempty"`
Type string `json:"type,omitempty"`
custom bool
error error
}
Expand All @@ -83,22 +82,11 @@ func (e *jsError) Error() string {
return e.error.Error()
}

func newJsUncaughtExceptionError(error error, st string, custom bool) *jsError {
jsErr := &jsError{
Type: "uncaughtExceptionError",
error: error,
custom: custom,
}
if !custom {
jsErr.StackTrace = st
}
return jsErr
}

func newJsRuntimeError(err error) *jsError {
func newJsError(error error, stackTrace string, custom bool) *jsError {
return &jsError{
Type: "runtimeError",
error: err,
error: error,
custom: custom,
StackTrace: stackTrace,
}
}

Expand Down Expand Up @@ -512,10 +500,10 @@ func (r *RuntimeJS) invokeFunction(execMode RuntimeExecutionMode, id string, fn
if !custom {
r.logger.Error("JavaScript runtime function raised an uncaught exception", zap.String("mode", execMode.String()), zap.String("id", id), zap.Error(err))
}
return nil, newJsUncaughtExceptionError(errors.New(errMsg), exErr.String(), custom), errCode
return nil, newJsError(errors.New(errMsg), exErr.String(), custom), errCode
}
r.logger.Error("JavaScript runtime function caused an error", zap.String("mode", execMode.String()), zap.String("id", id), zap.Error(err))
return nil, newJsRuntimeError(err), codes.Internal
r.logger.Error("JavaScript runtime error", zap.String("mode", execMode.String()), zap.String("id", id), zap.Error(err))
return nil, err, codes.Internal
}
if retVal == nil || retVal == goja.Undefined() || retVal == goja.Null() {
return nil, nil, codes.OK
Expand Down

0 comments on commit f4490d9

Please sign in to comment.