-
Notifications
You must be signed in to change notification settings - Fork 6
/
error_response.go
46 lines (38 loc) · 1.03 KB
/
error_response.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
/*
* Copyright (c) 2019 Entrust Datacard Corporation.
* All rights reserved.
*/
package main
import (
"encoding/json"
"fmt"
"github.com/pkg/errors"
)
type ErrorResponse struct {
Message Message `json:"message"`
Error Error `json:"error"`
}
type Error struct {
Code string `json:"code"`
Message string `json:"message"`
Target string `json:"target"`
Value string `json:"value"`
}
func CheckForError(b *backend, body []byte, statusCode int) error {
if statusCode != 200 {
if b.Logger().IsDebug() {
if statusCode >= 400 {
b.Logger().Debug(fmt.Sprintf("Received failure response code: %d", statusCode))
} else {
b.Logger().Debug(fmt.Sprintf("Received response code: %d", statusCode))
}
}
var errorResponse ErrorResponse
err := json.Unmarshal(body, &errorResponse)
if err != nil {
return errors.Wrap(err, fmt.Sprintf("CAGW error response could not be parsed (%d)", statusCode))
}
return errors.New(fmt.Sprintf("Error from gateway: %s (%d)", errorResponse.Error.Message, statusCode))
}
return nil
}