-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patherrors.go
64 lines (55 loc) · 1.94 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
package scrive
import (
"fmt"
"github.com/pkg/errors"
)
type ErrorType = string
const (
ErrorTypeServerError ErrorType = "server_error"
ErrorTypeEndpointNotFound ErrorType = "endpoint_not_found"
ErrorTypeInvalidAuthorisation ErrorType = "invalid_authorisation"
ErrorTypeInsufficientPrivileges ErrorType = "insufficient_privileges"
ErrorTypeResourceNotFound ErrorType = "resource_not_found"
ErrorTypeDocumentActionForbidden ErrorType = "document_action_forbidden"
ErrorTypeRequestParametersMissing ErrorType = "request_parameters_missing"
ErrorTypeRequestParametersParseError ErrorType = "request_parameters_parse_error"
ErrorTypeRequestParametersInvalid ErrorType = "request_parameters_invalid"
ErrorTypeDocumentObjectVersionMismatch ErrorType = "document_object_version_mismatch"
ErrorTypeSignatoryStateError ErrorType = "signatory_state_error"
ErrorTypeLocal ErrorType = "local_error"
)
type se struct {
ErrorType *ErrorType `json:"error_type"`
ErrorMessage *string `json:"error_message"`
HttpCode *int `json:"http_code"`
}
type ScriveError struct {
ErrorType ErrorType
ErrorMessage string
HttpCode int
}
func (c *Client) parseResponseError(body []byte) (*ScriveError, error) {
se := &se{}
if err := parseJson(body, se); err != nil {
logInvalidJson(body)
return nil, err
}
if anyNil(se.ErrorType, se.ErrorMessage, se.HttpCode) {
return nil, fmt.Errorf("Cannot parse: %s as ScriveError", string(body))
}
return &ScriveError{
ErrorType: *se.ErrorType,
ErrorMessage: *se.ErrorMessage,
HttpCode: *se.HttpCode,
}, nil
}
func localError(err error) *ScriveError {
return &ScriveError{
ErrorType: ErrorTypeLocal,
ErrorMessage: err.Error(),
HttpCode: -1,
}
}
func ScriveErrorToError(e *ScriveError) error {
return errors.Errorf("%d %s %s", e.HttpCode, string(e.ErrorType), e.ErrorMessage)
}