-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patherrors.go
66 lines (54 loc) · 1.64 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
package kit
import (
"encoding/json"
"fmt"
"io"
"net/http"
)
// ErrorResponse represents an error response
//
//nolint:errname // ErrorResponse is a valid name
type ErrorResponse struct {
StatusCode int `json:"-"` // HTTP status code, optional, not serialized
Err string `json:"error"` // Error message
}
// Error returns the error message
func (e ErrorResponse) Error() string {
return e.Err
}
// NewErrorResponse creates a new error response
func NewErrorResponse(err error, optionalStatusCode ...int) *ErrorResponse {
statusCode := http.StatusBadRequest
if len(optionalStatusCode) > 0 && optionalStatusCode[0] > 0 {
statusCode = optionalStatusCode[0]
}
if err == nil {
return &ErrorResponse{Err: "unknown error", StatusCode: statusCode}
}
return &ErrorResponse{Err: err.Error(), StatusCode: statusCode}
}
// MatrixError represents an error response from the Matrix API
type MatrixError struct {
Code string `json:"errcode"`
Err string `json:"error"`
}
// Error returns the error message
func (e MatrixError) Error() string {
return e.Err
}
// NewMatrixError creates a new Matrix error
func NewMatrixError(code, err string) *MatrixError {
return &MatrixError{Code: code, Err: err}
}
// MatrixErrorFrom creates a new Matrix error from io.Reader
func MatrixErrorFrom(r io.Reader) *MatrixError {
if r == nil {
return nil
}
var matrixErr *MatrixError
data, _ := io.ReadAll(r) //nolint:errcheck // ignore error as we will return nil
if err := json.Unmarshal(data, &matrixErr); err != nil {
return NewMatrixError("M_UNKNOWN", fmt.Sprintf("failed to decode error response %q: %v", string(data), err))
}
return matrixErr
}