generic.go 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. package exception
  2. import (
  3. "github.com/langgenius/dify-plugin-daemon/internal/types/entities"
  4. "github.com/langgenius/dify-plugin-daemon/internal/utils/parser"
  5. )
  6. type genericError struct {
  7. Message string `json:"message"`
  8. ErrorType string `json:"error_type"`
  9. Args map[string]any `json:"args"`
  10. code int
  11. }
  12. func (e *genericError) Error() string {
  13. return e.Message
  14. }
  15. func (e *genericError) ToResponse() *entities.Response {
  16. // TODO: using struct instead, currently, for compatibility with old code
  17. errorMsg := parser.MarshalJson(e)
  18. return entities.NewDaemonErrorResponse(e.code, errorMsg)
  19. }
  20. func Error(msg string) PluginDaemonError {
  21. return &genericError{Message: msg, code: -500, ErrorType: "unknown"}
  22. }
  23. func ErrorWithCode(msg string, code int) PluginDaemonError {
  24. return &genericError{Message: msg, code: code, ErrorType: "unknown"}
  25. }
  26. func ErrorWithType(msg string, errorType string) PluginDaemonError {
  27. return &genericError{Message: msg, code: -500, ErrorType: errorType}
  28. }
  29. func ErrorWithTypeAndCode(msg string, errorType string, code int) PluginDaemonError {
  30. return &genericError{Message: msg, code: code, ErrorType: errorType}
  31. }
  32. func ErrorWithTypeAndArgs(msg string, errorType string, args map[string]any) PluginDaemonError {
  33. return &genericError{Message: msg, code: -500, ErrorType: errorType, Args: args}
  34. }