Unverified Commit d25fa877 authored by JudGenie's avatar JudGenie Committed by GitHub
Browse files

Configure whether or not to add the Lua stacktrace in the API response. (#682)

parent 47f940ce
Loading
Loading
Loading
Loading
+2 −0
Original line number Diff line number Diff line
@@ -765,6 +765,7 @@ type RuntimeConfig struct {
	EventQueueWorkers  int               `yaml:"event_queue_workers" json:"event_queue_workers" usage:"Number of workers to use for concurrent processing of events. Default 8."`
	ReadOnlyGlobals    bool              `yaml:"read_only_globals" json:"read_only_globals" usage:"When enabled marks all Lua runtime global tables as read-only to reduce memory footprint. Default true."` // Kept for backwards compatibility
	LuaReadOnlyGlobals bool              `yaml:"lua_read_only_globals" json:"lua_read_only_globals" usage:"When enabled marks all Lua runtime global tables as read-only to reduce memory footprint. Default true."`
	LuaApiStackTrace   bool              `yaml:"lua_api_stacktrace" json:"lua_api_stacktrace" usage:"Add the Lua stacktrace in the API response"`
	JsEntrypoint       string            `yaml:"js_entrypoint" json:"js_entrypoint" usage:"Specifies the location of the bundled JavaScript runtime source code."`
}

@@ -825,6 +826,7 @@ func NewRuntimeConfig() *RuntimeConfig {
		EventQueueWorkers:  8,
		ReadOnlyGlobals:    true,
		LuaReadOnlyGlobals: true,
		LuaApiStackTrace:   true,
	}
}

+24 −70
Original line number Diff line number Diff line
@@ -1232,20 +1232,7 @@ func (rp *RuntimeProviderLua) Rpc(ctx context.Context, id string, queryParams ma
			code = 13
		}

		if apiErr, ok := fnErr.(*lua.ApiError); ok && !rp.logger.Core().Enabled(zapcore.InfoLevel) {
			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]
				}
			}
			return "", errors.New(msg), code
		}
		return "", fnErr, code
		return "", clearFnError(fnErr, rp, lf), code
	}

	if result == nil {
@@ -1298,20 +1285,7 @@ func (rp *RuntimeProviderLua) BeforeRt(ctx context.Context, id string, logger *z
			logger.Error("Runtime Before function caused an error.", zap.String("id", id), zap.Error(fnErr))
		}

		if apiErr, ok := fnErr.(*lua.ApiError); ok && !logger.Core().Enabled(zapcore.InfoLevel) {
			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]
				}
			}
			return nil, errors.New(msg)
		}
		return nil, fnErr
		return nil, clearFnError(fnErr, rp, lf)
	}

	if result == nil {
@@ -1370,20 +1344,7 @@ func (rp *RuntimeProviderLua) AfterRt(ctx context.Context, id string, logger *za
			logger.Error("Runtime After function caused an error.", zap.String("id", id), zap.Error(fnErr))
		}

		if apiErr, ok := fnErr.(*lua.ApiError); ok && !logger.Core().Enabled(zapcore.InfoLevel) {
			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]
				}
			}
			return errors.New(msg)
		}
		return fnErr
		return clearFnError(fnErr, rp, lf)
	}

	return nil
@@ -1438,20 +1399,7 @@ func (rp *RuntimeProviderLua) BeforeReq(ctx context.Context, id string, logger *
			logger.Error("Runtime Before function caused an error.", zap.String("id", id), zap.Error(fnErr))
		}

		if apiErr, ok := fnErr.(*lua.ApiError); ok && !logger.Core().Enabled(zapcore.InfoLevel) {
			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]
				}
			}
			return nil, errors.New(msg), code
		}
		return nil, fnErr, code
		return nil, clearFnError(fnErr, rp, lf), code
	}

	if result == nil || reqMap == nil {
@@ -1544,20 +1492,7 @@ func (rp *RuntimeProviderLua) AfterReq(ctx context.Context, id string, logger *z
			logger.Error("Runtime After function caused an error.", zap.String("id", id), zap.Error(fnErr))
		}

		if apiErr, ok := fnErr.(*lua.ApiError); ok && !logger.Core().Enabled(zapcore.InfoLevel) {
			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]
				}
			}
			return errors.New(msg)
		}
		return fnErr
		return clearFnError(fnErr, rp, lf)
	}

	return nil
@@ -2068,6 +2003,25 @@ func (r *RuntimeLua) Stop() {
	r.vm.Close()
}

func clearFnError(fnErr error, rp *RuntimeProviderLua, lf *lua.LFunction) error {
	if apiErr, ok := fnErr.(*lua.ApiError); ok &&
		(!rp.config.GetRuntime().LuaApiStackTrace || !rp.logger.Core().Enabled(zapcore.InfoLevel)) {

		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]
			}
		}
		return errors.New(msg)
	}
	return fnErr
}

func checkRuntimeLuaVM(logger *zap.Logger, config Config, stdLibs map[string]lua.LGFunction, moduleCache *RuntimeLuaModuleCache) error {
	vm := lua.NewState(lua.Options{
		CallStackSize:       config.GetRuntime().GetLuaCallStackSize(),