forked from adobe/ims-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patherror.go
62 lines (53 loc) · 1.92 KB
/
error.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
// Copyright 2019 Adobe. All rights reserved.
// This file is licensed to you under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License. You may obtain a copy
// of the License at http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software distributed under
// the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
// OF ANY KIND, either express or implied. See the License for the specific language
// governing permissions and limitations under the License.
package ims
import (
"encoding/json"
"fmt"
)
// Error is an error containing information returned by the IMS API.
type Error struct {
Response
// ErrorCode is an error code associated with the error response.
ErrorCode string
// ErrorMessage is a human-readable description of the error.
ErrorMessage string
}
func (e *Error) Error() string {
return fmt.Sprintf(
"error response: statusCode=%d, errorCode='%s', errorMessage='%s'",
e.StatusCode,
e.ErrorCode,
e.ErrorMessage,
)
}
// IsError checks if the given error is an IMS error and, if it is, returns an
// instance of Error.
func IsError(err error) (*Error, bool) {
imsErr, ok := err.(*Error)
return imsErr, ok
}
func errorResponse(res *Response) error {
var payload struct {
ErrorCode string `json:"error"`
ErrorMessage string `json:"error_description"`
}
// The error from json.Unmarshal() is voluntarily ignored. If the server
// returns an empty or badly serialized response, we just go on. The library
// did its best to extract meaningful information from the response. The
// unparsed body is returned to the user anyway, who will take the final
// decision about how to deal with this error.
_ = json.Unmarshal(res.Body, &payload)
return &Error{
Response: *res,
ErrorCode: payload.ErrorCode,
ErrorMessage: payload.ErrorMessage,
}
}