-
Notifications
You must be signed in to change notification settings - Fork 26
Expand file tree
/
Copy patherrors.go
More file actions
79 lines (64 loc) · 1.84 KB
/
errors.go
File metadata and controls
79 lines (64 loc) · 1.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
package schema
import (
"fmt"
"github.com/gofiber/fiber/v2"
)
type ErrorName = string
var (
UnsupportedMediaType ErrorName = "unsupported_media_type"
RouteNotFound ErrorName = "route_not_found"
PayloadParseError ErrorName = "payload_parse_error"
RouterNotFound ErrorName = "router_not_found"
NoModelConfigured ErrorName = "no_model_configured"
ModelUnavailable ErrorName = "model_unavailable"
AllModelsUnavailable ErrorName = "all_models_unavailable"
UnknownError ErrorName = "unknown_error"
)
// Error / Error contains more context than the built-in error type,
// so we know information like error code and message that are useful to propagate to clients
type Error struct {
Status int `json:"-"`
Name string `json:"name"`
Message string `json:"message"`
}
var _ error = (*Error)(nil)
// Error returns the error message.
func (e *Error) Error() string {
return fmt.Sprintf("Error (%s): %s", e.Name, e.Message)
}
func NewError(status int, name string, message string) Error {
return Error{Status: status, Name: name, Message: message}
}
var ErrUnsupportedMediaType = NewError(
fiber.StatusBadRequest,
UnsupportedMediaType,
"application/json is the only supported media type",
)
var ErrRouteNotFound = NewError(
fiber.StatusNotFound,
RouteNotFound,
"requested route is not found or method is not allowed",
)
var ErrRouterNotFound = NewError(fiber.StatusNotFound, RouterNotFound, "router is not found")
var ErrNoModelAvailable = NewError(
503,
AllModelsUnavailable,
"all providers are unavailable",
)
func NewPayloadParseErr(err error) Error {
return NewError(
fiber.StatusBadRequest,
PayloadParseError,
err.Error(),
)
}
func FromErr(err error) Error {
if apiErr, ok := err.(*Error); ok {
return *apiErr
}
return NewError(
fiber.StatusInternalServerError,
UnknownError,
err.Error(),
)
}