-
Notifications
You must be signed in to change notification settings - Fork 0
/
error.go
175 lines (159 loc) · 5.9 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
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
package s10s
import (
"encoding/json"
"errors"
"fmt"
"net/http"
api "github.com/ruudiRatlos/s10s/openapi"
)
type APIError int
func (e APIError) Error() string {
return fmt.Sprintf("API Error: %d", e)
}
func newAPIError(code int, msg string) error {
return fmt.Errorf("%s: %w", msg, APIError(code))
}
func (e APIError) Is(target error) bool {
var ae APIError
if errors.As(target, &ae) {
return int(e) == int(ae)
}
return false
}
type HTTPStatusError int
func (e HTTPStatusError) Error() string {
return fmt.Sprintf("API Error: %d", e)
}
func newHTTPStatusError(code int, msg string) error {
return fmt.Errorf("%s: %w", msg, HTTPStatusError(code))
}
func (e HTTPStatusError) Is(target error) bool {
var he HTTPStatusError
if errors.As(target, &he) {
return int(e) == int(he)
}
return false
}
func enhanceErr(err error, r *http.Response) error {
if r == nil || err == nil {
return err
}
defer func() { _ = r.Body.Close() }()
type JSONError struct {
Error struct {
Message string `json:"message"`
Code int `json:"code"`
Data struct {
ShipSymbol string `json:"shipSymbol"`
DestinationSymbol string `json:"destinationSymbol"`
Cooldown *api.Cooldown `json:"cooldown"`
} `json:"data"`
} `json:"error"`
}
if r.StatusCode > 299 {
err = errors.Join(err, newHTTPStatusError(r.StatusCode, r.Status))
}
dec := json.NewDecoder(r.Body)
var jsonMsg JSONError
jErr := dec.Decode(&jsonMsg)
if jErr != nil {
return errors.Join(jErr, err)
}
return errors.Join(newAPIError(jsonMsg.Error.Code, jsonMsg.Error.Message), err)
}
const ErrHTTPStatus429 HTTPStatusError = 429
const ErrHTTPStatus409 HTTPStatusError = 409
const ErrCooldownConflict APIError = 4000
const ErrWaypointNoAccess APIError = 4001
const ErrTokenEmpty APIError = 4100
const ErrTokenMissingSubject APIError = 4101
const ErrTokenInvalidSubject APIError = 4102
const ErrMissingTokenRequest APIError = 4103
const ErrInvalidTokenRequest APIError = 4104
const ErrInvalidTokenSubject APIError = 4105
const ErrAccountNotExists APIError = 4106
const ErrAgentNotExists APIError = 4107
const ErrAccountHasNoAgent APIError = 4108
const ErrRegisterAgentExists APIError = 4109
const ErrRegisterAgentSymbolReserved APIError = 4110
const ErrRegisterAgentConflictSymbol APIError = 4111
const ErrNavigateInTransit APIError = 4200
const ErrNavigateInvalidDestination APIError = 4201
const ErrNavigateOutsideSystem APIError = 4202
const ErrNavigateInsufficientFuel APIError = 4203
const ErrNavigateSameDestination APIError = 4204
const ErrShipExtractInvalidWaypoint APIError = 4205
const ErrShipExtractPermission APIError = 4206
const ErrShipJumpNoSystem APIError = 4207
const ErrShipJumpSameSystem APIError = 4208
const ErrShipJumpMissingModule APIError = 4210
const ErrShipJumpNoValidWaypoint APIError = 4211
const ErrShipJumpMissingAntimatter APIError = 4212
const ErrShipInTransit APIError = 4214
const ErrShipMissingSensorArrays APIError = 4215
const ErrPurchaseShipCredits APIError = 4216
const ErrShipCargoExceedsLimit APIError = 4217
const ErrShipCargoMissing APIError = 4218
const ErrShipCargoUnitCount APIError = 4219
const ErrShipSurveyVerification APIError = 4220
const ErrShipSurveyExpiration APIError = 4221
const ErrShipSurveyWaypointType APIError = 4222
const ErrShipSurveyOrbit APIError = 4223
const ErrShipSurveyExhausted APIError = 4224
const ErrShipRefuelDocked APIError = 4225
const ErrShipRefuelInvalidWaypoint APIError = 4226
const ErrShipMissingMounts APIError = 4227
const ErrShipCargoFull APIError = 4228
const ErrShipJumpFromGateToGate APIError = 4229
const ErrWaypointCharted APIError = 4230
const ErrShipTransferShipNotFound = 4231
const ErrShipTransferAgentConflict = 4232
const ErrShipTransferSameShipConflict = 4233
const ErrShipTransferLocationConflict = 4234
const ErrWarpInsideSystem APIError = 4235
const ErrShipNotInOrbit APIError = 4236
const ErrShipInvalidRefineryGood APIError = 4237
const ErrShipInvalidRefineryType APIError = 4238
const ErrShipMissingRefinery APIError = 4239
const ErrShipMissingSurveyor APIError = 4240
const ErrShipMissingWarpDrive APIError = 4241
const ErrShipMissingMineralProcessor APIError = 4242
const ErrShipMissingMiningLasers APIError = 4243
const ErrShipNotDocked APIError = 4244
const ErrPurchaseShipNotPresent APIError = 4245
const ErrShipMountNoShipyard APIError = 4246
const ErrShipMissingMount APIError = 4247
const ErrShipMountInsufficientCredits APIError = 4248
const ErrShipMissingPower APIError = 4249
const ErrShipMissingSlots APIError = 4250
const ErrShipMissingCrew APIError = 4252
const ErrShipExtractDestabilized APIError = 4253
const ErrShipJumpInvalidOrigin APIError = 4254
const ErrShipJumpInvalidWaypoint APIError = 4255
const ErrShipJumpOriginUnderConstruction APIError = 4256
const ErrShipMissingGasProcessor APIError = 4257
const ErrShipMissingGasSiphons APIError = 4258
const ErrShipSiphonInvalidWaypoint APIError = 4259
const ErrShipSiphonPermission APIError = 4260
const ErrWaypointNoYield APIError = 4261
const ErrShipJumpDestinationUnderConstruction APIError = 4262
const ErrAcceptContractNotAuthorized APIError = 4500
const ErrAcceptContractConflict APIError = 4501
const ErrFulfillContractDelivery APIError = 4502
const ErrContractDeadline APIError = 4503
const ErrContractFulfilled APIError = 4504
const ErrContractNotAccepted APIError = 4505
const ErrContractNotAuthorized APIError = 4506
const ErrShipDeliverTerms APIError = 4508
const ErrShipDeliverFulfilled APIError = 4509
const ErrShipDeliverInvalidLocation APIError = 4510
const ErrExistingContract APIError = 4511
const ErrMarketTradeInsufficientCredits APIError = 4600
const ErrMarketTradeNoPurchase APIError = 4601
const ErrMarketTradeNotSold APIError = 4602
const ErrMarketNotFound APIError = 4603
const ErrMarketTradeUnitLimit APIError = 4604
const ErrWaypointNoFaction APIError = 4700
const ErrConstructionMaterialNotRequired = 4800
const ErrConstructionMaterialFulfilled = 4801
const ErrShipConstructionInvalidLocation APIError = 4802