File: errors.go

package info (click to toggle)
golang-github-akamai-akamaiopen-edgegrid-golang 1.0.1-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, forky, sid, trixie
  • size: 1,408 kB
  • sloc: sh: 532; makefile: 3
file content (107 lines) | stat: -rw-r--r-- 3,280 bytes parent folder | download
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
package client

import (
	"fmt"
	"io/ioutil"
	"net/http"
	"strings"

	"github.com/akamai/AkamaiOPEN-edgegrid-golang/jsonhooks-v1"
)

// APIError exposes an Akamai OPEN Edgegrid Error
type APIError struct {
	error
	Type        string           `json:"type"`
	Title       string           `json:"title"`
	Status      int              `json:"status"`
	Detail      string           `json:"detail"`
	Errors      []APIErrorDetail `json:"errors"`
	Problems    []APIErrorDetail `json:"problems"`
	Instance    string           `json:"instance"`
	Method      string           `json:"method"`
	ServerIP    string           `json:"serverIp"`
	ClientIP    string           `json:"clientIp"`
	RequestID   string           `json:"requestId"`
	RequestTime string           `json:"requestTime"`
	Response    *http.Response   `json:"-"`
	RawBody     string           `json:"-"`
}

type APIErrorDetail struct {
	Type          string `json:"type"`
	Title         string `json:"title"`
	Detail        string `json:"detail"`
	RejectedValue string `json:"rejectedValue"`
}

func (error APIError) Error() string {
	var errorDetails string
	if len(error.Errors) > 0 {
		for _, e := range error.Errors {
			errorDetails = fmt.Sprintf("%s \n %s", errorDetails, e)
		}
	}
	if len(error.Problems) > 0 {
		for _, e := range error.Problems {
			errorDetails = fmt.Sprintf("%s \n %s", errorDetails, e)
		}
	}
	return strings.TrimSpace(fmt.Sprintf("API Error: %d %s %s More Info %s\n %s", error.Status, error.Title, error.Detail, error.Type, errorDetails))
}

// NewAPIError creates a new API error based on a Response,
// or http.Response-like.
func NewAPIError(response *http.Response) APIError {
	// TODO: handle this error
	body, _ := ioutil.ReadAll(response.Body)

	return NewAPIErrorFromBody(response, body)
}

// NewAPIErrorFromBody creates a new API error, allowing you to pass in a body
//
// This function is intended to be used after the body has already been read for
// other purposes.
func NewAPIErrorFromBody(response *http.Response, body []byte) APIError {
	error := APIError{}
	if err := jsonhooks.Unmarshal(body, &error); err != nil {
		error.Status = response.StatusCode
		error.Title = response.Status
	}

	error.Response = response
	error.RawBody = string(body)

	return error
}

// IsInformational determines if a response was informational (1XX status)
func IsInformational(r *http.Response) bool {
	return r.StatusCode > 99 && r.StatusCode < 200
}

// IsSuccess determines if a response was successful (2XX status)
func IsSuccess(r *http.Response) bool {
	return r.StatusCode > 199 && r.StatusCode < 300
}

// IsRedirection determines if a response was a redirect (3XX status)
func IsRedirection(r *http.Response) bool {
	return r.StatusCode > 299 && r.StatusCode < 400
}

// IsClientError determines if a response was a client error (4XX status)
func IsClientError(r *http.Response) bool {
	return r.StatusCode > 399 && r.StatusCode < 500
}

// IsServerError determines if a response was a server error (5XX status)
func IsServerError(r *http.Response) bool {
	return r.StatusCode > 499 && r.StatusCode < 600
}

// IsError determines if the response was a client or server error (4XX or 5XX status)
func IsError(r *http.Response) bool {
	return r.StatusCode > 399 && r.StatusCode < 600
}