File size: 2,498 Bytes
8e80efe
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
package core

import (
	"encoding/json"
	"net/http"
	"strings"
)

type ErrorType string

const (
	ErrorTypeAPI      ErrorType = "api_error"
	ErrorTypeStore    ErrorType = "store_error"
	ErrorTypeUpstream ErrorType = "upstream_error"
	ErrorTypeUnknown  ErrorType = "unknown_error"
)

type ErrorCode string

const (
	ErrorCodeBadRequest         ErrorCode = "BAD_REQUEST"
	ErrorCodeMagnetInvalidId    ErrorCode = "MAGNET_INVALID_ID"
	ErrorCodeMagnetInvalidURI   ErrorCode = "MAGNET_INVALID_URI"
	ErrorCodeStoreLimitExceeded ErrorCode = "STORE_LIMIT_EXCEEDED"
)

type StremThruError interface {
	Pack()
	GetStatusCode() int
	GetError() *Error
}

type Error struct {
	Type ErrorType `json:"type"`

	Code ErrorCode `json:"code,omitempty"`
	Msg  string    `json:"message"`

	Method     string `json:"method,omitempty"`
	Path       string `json:"path,omitempty"`
	StatusCode int    `json:"status_code,omitempty"`

	StoreName     string `json:"store_name,omitempty"`
	UpstreamCause error  `json:"__upstream_cause__,omitempty"`

	Cause error `json:"__cause__,omitempty"`
}

func (e *Error) Error() string {
	ret, _ := json.Marshal(e)
	return string(ret)
}

func (e *Error) Unwrap() error {
	return e.Cause
}

func (e *Error) InjectReq(r *http.Request) {
	e.Method = r.Method
	e.Path = r.URL.Path
	if storeName := r.Header.Get("X-StremThru-Store-Name"); storeName != "" {
		e.StoreName = storeName
	}
}

func (e *Error) Pack() {
	if e.StatusCode == 0 {
		e.StatusCode = 500
	}
	if e.Msg == "" {
		if e.Cause != nil {
			e.Msg = e.Cause.Error()
		} else if e.UpstreamCause != nil {
			e.Msg = e.UpstreamCause.Error()
		}
	}
}

func (e *Error) GetStatusCode() int {
	return e.StatusCode
}

func (e *Error) GetError() *Error {
	return e
}

func NewError(msg string) *Error {
	err := &Error{}
	err.Type = ErrorTypeUnknown
	err.Msg = msg
	return err
}

type err = Error

type APIError struct {
	err
}

func NewAPIError(msg string) *APIError {
	err := &APIError{}
	err.Type = ErrorTypeAPI
	err.Msg = msg
	return err
}

type StoreError struct {
	err
}

func NewStoreError(msg string) *StoreError {
	err := &StoreError{}
	err.Type = ErrorTypeStore
	err.Msg = msg
	return err
}

type UpstreamError struct {
	err
}

func (e *UpstreamError) Pack() {
	e.err.Pack()

	if e.StoreName != "" && e.Code != "" {
		e.Code = ErrorCode(strings.ToUpper(e.StoreName) + "_" + string(e.Code))
	}
}

func NewUpstreamError(msg string) *UpstreamError {
	err := &UpstreamError{}
	err.Type = ErrorTypeUpstream
	err.Msg = msg
	return err
}