-
Notifications
You must be signed in to change notification settings - Fork 10
/
stytcherror.go
52 lines (42 loc) · 1.16 KB
/
stytcherror.go
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
package stytcherror
import (
"fmt"
"strings"
"github.com/stytchauth/stytch-go/v6/stytch/config"
)
type Error struct {
StatusCode int `json:"status_code,omitempty"`
RequestID string `json:"request_id,omitempty"`
ErrorType Type `json:"error_type,omitempty"`
ErrorMessage Message `json:"error_message,omitempty"`
ErrorURL URL `json:"error_url,omitempty"`
}
type (
Type string
Message string
URL string
)
func (e Error) Error() string {
var es strings.Builder
es.WriteString("Stytch Error - ")
if e.RequestID != "" {
fmt.Fprintf(&es, "request ID: %s, ", e.RequestID)
}
fmt.Fprintf(&es, "status code: %d, type: %s, message: %s",
e.StatusCode, e.ErrorType, e.ErrorMessage)
if e.ErrorURL != "" {
fmt.Fprintf(&es, " error_url: %s", e.ErrorURL)
}
return es.String()
}
func NewClientLibraryError(message string) error {
if message == "" {
message = "Oops, something seems to have gone wrong with the Stytch Go client library"
}
return Error{
StatusCode: 500,
ErrorType: "client_library_error",
ErrorMessage: Message(message + ", v" + config.APIVersion),
ErrorURL: "https://stytch.com/docs/api/errors/500",
}
}