-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy patherrors.go
90 lines (80 loc) · 1.95 KB
/
errors.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
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
80
81
82
83
84
85
86
87
88
89
90
package openai
import (
"io"
"net/http"
"github.com/Simplou/goxios"
)
var (
internalError = func(err error, t string) *OpenAIErr {
return NewOpenAIErr(err, 500, t)
}
errCannotOpenFile = func(err error) *OpenAIErr {
return internalError(err, "cannot_open_file")
}
errCannotCreateFormFile = func(err error) *OpenAIErr {
return internalError(err, "cannot_create_form_file")
}
errCannotCopyFileContent = func(err error) *OpenAIErr {
return internalError(err, "cannot_copy_file_content")
}
errCannotSendRequest = func(err error) *OpenAIErr {
return internalError(err, "cannot_send_request")
}
errCannotDecodeJSON = func(err error) *OpenAIErr {
return internalError(err, "cannot_decode_json")
}
errCannotMarshalJSON = func(err error) *OpenAIErr {
return internalError(err, "cannot_marshal_json")
}
errCloseBody = func(err error) *OpenAIErr {
return internalError(err, "close_body_error")
}
)
type OpenAIErr struct {
Err JSONErr `json:"error"`
status int
}
func (o *OpenAIErr) Error() string {
return o.Err.Message
}
func (o *OpenAIErr) Status() int {
return o.status
}
func NewOpenAIErr(err error, statusCode int, t string) *OpenAIErr {
if err != nil {
return &OpenAIErr{
Err: JSONErr{
Message: err.Error(),
Type: t,
},
status: statusCode,
}
}
return nil
}
func closeBody(body io.ReadCloser, err error) *OpenAIErr {
if err := body.Close(); err != nil {
return errCloseBody(err)
}
if err, ok := err.(*OpenAIErr); ok {
return err
}
if err != nil {
return internalError(err, "internal_error")
}
return nil
}
func openaiHttpError(res *http.Response) *OpenAIErr {
err := new(OpenAIErr)
if err := goxios.DecodeJSON(res.Body, err); err != nil {
return closeBody(res.Body, err)
}
err.status = res.StatusCode
return closeBody(res.Body, err)
}
type JSONErr struct {
Message string `json:"message"`
Type string `json:"type"`
Param string `json:"param,omitempty"`
Code string `json:"code,omitempty"`
}