| 1 |
package errors |
| 2 |
|
| 3 |
import ( |
| 4 |
"encoding/json" |
| 5 |
"fmt" |
| 6 |
"io" |
| 7 |
"net/http" |
| 8 |
|
| 9 |
"github.com/go-ap/jsonld" |
| 10 |
) |
| 11 |
|
| 12 |
const errorsPackageName = "github.com/go-ap/errors" |
| 13 |
const runtimeDebugPackageName = "runtime/debug" |
| 14 |
|
| 15 |
type Error interface { |
| 16 |
error |
| 17 |
json.Unmarshaler |
| 18 |
} |
| 19 |
|
| 20 |
type notFound struct { |
| 21 |
Err |
| 22 |
s int |
| 23 |
} |
| 24 |
|
| 25 |
type methodNotAllowed struct { |
| 26 |
Err |
| 27 |
s int |
| 28 |
} |
| 29 |
|
| 30 |
type notValid struct { |
| 31 |
Err |
| 32 |
s int |
| 33 |
} |
| 34 |
|
| 35 |
type forbidden struct { |
| 36 |
Err |
| 37 |
s int |
| 38 |
} |
| 39 |
|
| 40 |
type notImplemented struct { |
| 41 |
Err |
| 42 |
s int |
| 43 |
} |
| 44 |
|
| 45 |
type conflict struct { |
| 46 |
Err |
| 47 |
s int |
| 48 |
} |
| 49 |
|
| 50 |
type gone struct { |
| 51 |
Err |
| 52 |
s int |
| 53 |
} |
| 54 |
|
| 55 |
type unsupportedMediaType struct { |
| 56 |
Err |
| 57 |
s int |
| 58 |
} |
| 59 |
|
| 60 |
type badRequest struct { |
| 61 |
Err |
| 62 |
s int |
| 63 |
} |
| 64 |
|
| 65 |
type unauthorized struct { |
| 66 |
Err |
| 67 |
s int |
| 68 |
challenge string |
| 69 |
} |
| 70 |
|
| 71 |
type notSupportedVersion struct { |
| 72 |
Err |
| 73 |
s int |
| 74 |
} |
| 75 |
|
| 76 |
type timeout struct { |
| 77 |
Err |
| 78 |
s int |
| 79 |
} |
| 80 |
|
| 81 |
type badGateway struct { |
| 82 |
Err |
| 83 |
s int |
| 84 |
} |
| 85 |
|
| 86 |
type serviceUnavailable struct { |
| 87 |
Err |
| 88 |
s int |
| 89 |
} |
| 90 |
|
| 91 |
func wrapErr(err error, s string, args ...any) Err { |
| 92 |
e := Annotatef(err, s, args...) |
| 93 |
asErr := Err{} |
| 94 |
As(e, &asErr) |
| 95 |
return asErr |
| 96 |
} |
| 97 |
|
| 98 |
func FromResponse(resp *http.Response) error { |
| 99 |
if resp.StatusCode < http.StatusBadRequest { |
| 100 |
return nil |
| 101 |
} |
| 102 |
body := make([]byte, 0) |
| 103 |
defer resp.Body.Close() |
| 104 |
|
| 105 |
body, _ = io.ReadAll(resp.Body) |
| 106 |
|
| 107 |
errors, err := UnmarshalJSON(body) |
| 108 |
if err != nil { |
| 109 |
return AnnotateFromStatus(nil, resp.StatusCode, "test") |
| 110 |
} |
| 111 |
if len(errors) == 0 { |
| 112 |
return nil |
| 113 |
} |
| 114 |
|
| 115 |
return AnnotateFromStatus(Join(errors...), resp.StatusCode, "test") |
| 116 |
} |
| 117 |
|
| 118 |
func AnnotateFromStatus(err error, status int, s string, args ...any) error { |
| 119 |
switch status { |
| 120 |
case http.StatusNotModified: |
| 121 |
return NewNotModified(err, fmt.Sprintf(s, args...)) |
| 122 |
case http.StatusBadRequest: |
| 123 |
return NewBadRequest(err, s, args...) |
| 124 |
case http.StatusUnauthorized: |
| 125 |
return NewUnauthorized(err, s, args...) |
| 126 |
|
| 127 |
case http.StatusForbidden: |
| 128 |
return NewForbidden(err, s, args...) |
| 129 |
case http.StatusNotFound: |
| 130 |
return NewNotFound(err, s, args...) |
| 131 |
case http.StatusMethodNotAllowed: |
| 132 |
return NewMethodNotAllowed(err, s, args...) |
| 133 |
case http.StatusNotAcceptable: |
| 134 |
return NewNotValid(err, s, args...) |
| 135 |
|
| 136 |
|
| 137 |
case http.StatusConflict: |
| 138 |
return NewConflict(err, s, args...) |
| 139 |
case http.StatusGone: |
| 140 |
return NewGone(err, s, args...) |
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
case http.StatusUnsupportedMediaType: |
| 146 |
return NewUnsupportedMedia(err, s, args...) |
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
case http.StatusNotImplemented: |
| 160 |
return NewNotImplemented(err, s, args...) |
| 161 |
case http.StatusBadGateway: |
| 162 |
return NewBadGateway(err, s, args...) |
| 163 |
|
| 164 |
|
| 165 |
case http.StatusHTTPVersionNotSupported: |
| 166 |
return NewNotSupported(err, s, args...) |
| 167 |
case http.StatusGatewayTimeout: |
| 168 |
return NewTimeout(err, s, args...) |
| 169 |
} |
| 170 |
return Annotatef(err, s, args...) |
| 171 |
} |
| 172 |
|
| 173 |
func NewFromStatus(status int, s string, args ...any) error { |
| 174 |
switch status { |
| 175 |
case http.StatusBadRequest: |
| 176 |
return BadRequestf(s, args...) |
| 177 |
case http.StatusUnauthorized: |
| 178 |
return Unauthorizedf(s, args...) |
| 179 |
|
| 180 |
case http.StatusForbidden: |
| 181 |
return Forbiddenf(s, args...) |
| 182 |
case http.StatusNotFound: |
| 183 |
return NotFoundf(s, args...) |
| 184 |
case http.StatusMethodNotAllowed: |
| 185 |
return MethodNotAllowedf(s, args...) |
| 186 |
case http.StatusNotAcceptable: |
| 187 |
return NotValidf(s, args...) |
| 188 |
|
| 189 |
|
| 190 |
case http.StatusConflict: |
| 191 |
return Conflictf(s, args...) |
| 192 |
case http.StatusGone: |
| 193 |
return Gonef(s, args...) |
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
case http.StatusNotImplemented: |
| 212 |
return NotImplementedf(s, args...) |
| 213 |
case http.StatusBadGateway: |
| 214 |
return BadGatewayf(s, args...) |
| 215 |
|
| 216 |
|
| 217 |
case http.StatusHTTPVersionNotSupported: |
| 218 |
return NotSupportedf(s, args...) |
| 219 |
case http.StatusGatewayTimeout: |
| 220 |
return Timeoutf(s, args...) |
| 221 |
} |
| 222 |
return Newf(s, args...) |
| 223 |
} |
| 224 |
|
| 225 |
func WrapWithStatus(status int, err error, s string, args ...any) error { |
| 226 |
switch status { |
| 227 |
case http.StatusBadRequest: |
| 228 |
return NewBadRequest(err, s, args...) |
| 229 |
case http.StatusUnauthorized: |
| 230 |
return NewUnauthorized(err, s, args...) |
| 231 |
|
| 232 |
case http.StatusForbidden: |
| 233 |
return NewForbidden(err, s, args...) |
| 234 |
case http.StatusNotFound: |
| 235 |
return NewNotFound(err, s, args...) |
| 236 |
case http.StatusMethodNotAllowed: |
| 237 |
return NewMethodNotAllowed(err, s, args...) |
| 238 |
case http.StatusNotAcceptable: |
| 239 |
return NewNotValid(err, s, args...) |
| 240 |
|
| 241 |
|
| 242 |
case http.StatusConflict: |
| 243 |
return NewConflict(err, s, args...) |
| 244 |
case http.StatusGone: |
| 245 |
return NewGone(err, s, args...) |
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
case http.StatusNotImplemented: |
| 264 |
return NewNotImplemented(err, s, args...) |
| 265 |
case http.StatusBadGateway: |
| 266 |
return NewBadGateway(err, s, args...) |
| 267 |
case http.StatusServiceUnavailable: |
| 268 |
return NewServiceUnavailable(err, s, args...) |
| 269 |
|
| 270 |
case http.StatusHTTPVersionNotSupported: |
| 271 |
return NewNotSupported(err, s, args...) |
| 272 |
case http.StatusGatewayTimeout: |
| 273 |
return NewTimeout(err, s, args...) |
| 274 |
} |
| 275 |
return wrapErr(err, s, args...) |
| 276 |
} |
| 277 |
func NotFoundf(s string, args ...any) *notFound { |
| 278 |
return ¬Found{Err: wrapErr(nil, s, args...), s: http.StatusNotFound} |
| 279 |
} |
| 280 |
func NewNotFound(e error, s string, args ...any) *notFound { |
| 281 |
return ¬Found{Err: wrapErr(e, s, args...), s: http.StatusNotFound} |
| 282 |
} |
| 283 |
func MethodNotAllowedf(s string, args ...any) *methodNotAllowed { |
| 284 |
return &methodNotAllowed{Err: wrapErr(nil, s, args...), s: http.StatusMethodNotAllowed} |
| 285 |
} |
| 286 |
func NewMethodNotAllowed(e error, s string, args ...any) *methodNotAllowed { |
| 287 |
return &methodNotAllowed{Err: wrapErr(e, s, args...), s: http.StatusMethodNotAllowed} |
| 288 |
} |
| 289 |
func NotValidf(s string, args ...any) *notValid { |
| 290 |
return ¬Valid{Err: wrapErr(nil, s, args...)} |
| 291 |
} |
| 292 |
func NewNotValid(e error, s string, args ...any) *notValid { |
| 293 |
return ¬Valid{Err: wrapErr(e, s, args...)} |
| 294 |
} |
| 295 |
func Conflictf(s string, args ...any) *conflict { |
| 296 |
return &conflict{Err: wrapErr(nil, s, args...), s: http.StatusConflict} |
| 297 |
} |
| 298 |
func NewConflict(e error, s string, args ...any) *conflict { |
| 299 |
return &conflict{Err: wrapErr(e, s, args...), s: http.StatusConflict} |
| 300 |
} |
| 301 |
func Gonef(s string, args ...any) *gone { |
| 302 |
return &gone{Err: wrapErr(nil, s, args...), s: http.StatusGone} |
| 303 |
} |
| 304 |
func NewGone(e error, s string, args ...any) *gone { |
| 305 |
return &gone{Err: wrapErr(e, s, args...), s: http.StatusGone} |
| 306 |
} |
| 307 |
func Forbiddenf(s string, args ...any) *forbidden { |
| 308 |
return &forbidden{Err: wrapErr(nil, s, args...), s: http.StatusForbidden} |
| 309 |
} |
| 310 |
func NewForbidden(e error, s string, args ...any) *forbidden { |
| 311 |
return &forbidden{Err: wrapErr(e, s, args...), s: http.StatusForbidden} |
| 312 |
} |
| 313 |
func NotImplementedf(s string, args ...any) *notImplemented { |
| 314 |
return ¬Implemented{Err: wrapErr(nil, s, args...), s: http.StatusNotImplemented} |
| 315 |
} |
| 316 |
func NewNotImplemented(e error, s string, args ...any) *notImplemented { |
| 317 |
return ¬Implemented{Err: wrapErr(e, s, args...), s: http.StatusNotImplemented} |
| 318 |
} |
| 319 |
func BadRequestf(s string, args ...any) *badRequest { |
| 320 |
return &badRequest{Err: wrapErr(nil, s, args...), s: http.StatusBadRequest} |
| 321 |
} |
| 322 |
func NewBadRequest(e error, s string, args ...any) *badRequest { |
| 323 |
return &badRequest{Err: wrapErr(e, s, args...), s: http.StatusBadRequest} |
| 324 |
} |
| 325 |
func Unauthorizedf(s string, args ...any) *unauthorized { |
| 326 |
return &unauthorized{Err: wrapErr(nil, s, args...), s: http.StatusUnauthorized} |
| 327 |
} |
| 328 |
func NewUnauthorized(e error, s string, args ...any) *unauthorized { |
| 329 |
return &unauthorized{Err: wrapErr(e, s, args...), s: http.StatusUnauthorized} |
| 330 |
} |
| 331 |
func UnsupportedMediaTypef(s string, args ...any) *unsupportedMediaType { |
| 332 |
return &unsupportedMediaType{Err: wrapErr(nil, s, args...), s: http.StatusUnsupportedMediaType} |
| 333 |
} |
| 334 |
func NewUnsupportedMedia(e error, s string, args ...any) *unsupportedMediaType { |
| 335 |
return &unsupportedMediaType{Err: wrapErr(e, s, args...), s: http.StatusGone} |
| 336 |
} |
| 337 |
func NotSupportedf(s string, args ...any) *notSupportedVersion { |
| 338 |
return ¬SupportedVersion{Err: wrapErr(nil, s, args...), s: http.StatusHTTPVersionNotSupported} |
| 339 |
} |
| 340 |
func NewNotSupported(e error, s string, args ...any) *notSupportedVersion { |
| 341 |
return ¬SupportedVersion{Err: wrapErr(e, s, args...), s: http.StatusHTTPVersionNotSupported} |
| 342 |
} |
| 343 |
func Timeoutf(s string, args ...any) *timeout { |
| 344 |
return &timeout{Err: wrapErr(nil, s, args...), s: http.StatusRequestTimeout} |
| 345 |
} |
| 346 |
func NewTimeout(e error, s string, args ...any) *timeout { |
| 347 |
return &timeout{Err: wrapErr(e, s, args...), s: http.StatusRequestTimeout} |
| 348 |
} |
| 349 |
func BadGatewayf(s string, args ...any) *badGateway { |
| 350 |
return &badGateway{Err: wrapErr(nil, s, args...), s: http.StatusBadGateway} |
| 351 |
} |
| 352 |
func NewBadGateway(e error, s string, args ...any) *badGateway { |
| 353 |
return &badGateway{Err: wrapErr(e, s, args...), s: http.StatusBadGateway} |
| 354 |
} |
| 355 |
func ServiceUnavailablef(s string, args ...any) *serviceUnavailable { |
| 356 |
return &serviceUnavailable{Err: wrapErr(nil, s, args...), s: http.StatusServiceUnavailable} |
| 357 |
} |
| 358 |
func NewServiceUnavailable(e error, s string, args ...any) *serviceUnavailable { |
| 359 |
return &serviceUnavailable{Err: wrapErr(e, s, args...), s: http.StatusServiceUnavailable} |
| 360 |
} |
| 361 |
func IsServiceUnavailable(e error) bool { |
| 362 |
_, okp := e.(*serviceUnavailable) |
| 363 |
_, oks := e.(serviceUnavailable) |
| 364 |
return okp || oks || As(e, &serviceUnavailable{}) |
| 365 |
} |
| 366 |
func IsBadRequest(e error) bool { |
| 367 |
_, okp := e.(*badRequest) |
| 368 |
_, oks := e.(badRequest) |
| 369 |
return okp || oks || As(e, &badRequest{}) |
| 370 |
} |
| 371 |
func IsForbidden(e error) bool { |
| 372 |
_, okp := e.(*forbidden) |
| 373 |
_, oks := e.(forbidden) |
| 374 |
return okp || oks || As(e, &forbidden{}) |
| 375 |
} |
| 376 |
func IsNotSupported(e error) bool { |
| 377 |
_, okp := e.(*notSupportedVersion) |
| 378 |
_, oks := e.(notSupportedVersion) |
| 379 |
return okp || oks |
| 380 |
} |
| 381 |
func IsConflict(e error) bool { |
| 382 |
_, okp := e.(*conflict) |
| 383 |
_, oks := e.(conflict) |
| 384 |
return okp || oks || As(e, &conflict{}) |
| 385 |
} |
| 386 |
func IsGone(e error) bool { |
| 387 |
_, okp := e.(*gone) |
| 388 |
_, oks := e.(gone) |
| 389 |
return okp || oks || As(e, &gone{}) |
| 390 |
} |
| 391 |
func IsUnsupportedMedia(e error) bool { |
| 392 |
_, okp := e.(*unsupportedMediaType) |
| 393 |
_, oks := e.(unsupportedMediaType) |
| 394 |
return okp || oks || As(e, &unsupportedMediaType{}) |
| 395 |
} |
| 396 |
func IsMethodNotAllowed(e error) bool { |
| 397 |
_, okp := e.(*methodNotAllowed) |
| 398 |
_, oks := e.(methodNotAllowed) |
| 399 |
return okp || oks || As(e, &methodNotAllowed{}) |
| 400 |
} |
| 401 |
func IsNotFound(e error) bool { |
| 402 |
_, okp := e.(*notFound) |
| 403 |
_, oks := e.(notFound) |
| 404 |
return okp || oks || As(e, ¬Found{}) |
| 405 |
} |
| 406 |
func IsNotImplemented(e error) bool { |
| 407 |
_, okp := e.(*notImplemented) |
| 408 |
_, oks := e.(notImplemented) |
| 409 |
return okp || oks || As(e, ¬Implemented{}) |
| 410 |
} |
| 411 |
func IsUnauthorized(e error) bool { |
| 412 |
_, okp := e.(*unauthorized) |
| 413 |
_, oks := e.(unauthorized) |
| 414 |
return okp || oks || As(e, &unauthorized{}) |
| 415 |
} |
| 416 |
func IsTimeout(e error) bool { |
| 417 |
_, okp := e.(*timeout) |
| 418 |
_, oks := e.(timeout) |
| 419 |
return okp || oks || As(e, &timeout{}) |
| 420 |
} |
| 421 |
func IsNotValid(e error) bool { |
| 422 |
_, okp := e.(*notValid) |
| 423 |
_, oks := e.(notValid) |
| 424 |
return okp || oks || As(e, ¬Valid{}) |
| 425 |
} |
| 426 |
|
| 427 |
func IsBadGateway(e error) bool { |
| 428 |
_, okp := e.(*badGateway) |
| 429 |
_, oks := e.(badGateway) |
| 430 |
return okp || oks || As(e, &badGateway{}) |
| 431 |
} |
| 432 |
func (n notFound) Is(e error) bool { |
| 433 |
return IsNotFound(e) |
| 434 |
} |
| 435 |
func (n notValid) Is(e error) bool { |
| 436 |
return IsNotValid(e) |
| 437 |
} |
| 438 |
func (n notImplemented) Is(e error) bool { |
| 439 |
return IsNotImplemented(e) |
| 440 |
} |
| 441 |
func (n notSupportedVersion) Is(e error) bool { |
| 442 |
return IsNotSupported(e) |
| 443 |
} |
| 444 |
func (b badRequest) Is(e error) bool { |
| 445 |
return IsBadRequest(e) |
| 446 |
} |
| 447 |
func (s serviceUnavailable) Is(e error) bool { |
| 448 |
return IsServiceUnavailable(e) |
| 449 |
} |
| 450 |
func (t timeout) Is(e error) bool { |
| 451 |
return IsTimeout(e) |
| 452 |
} |
| 453 |
func (u unauthorized) Is(e error) bool { |
| 454 |
return IsUnauthorized(e) |
| 455 |
} |
| 456 |
func (m methodNotAllowed) Is(e error) bool { |
| 457 |
return IsMethodNotAllowed(e) |
| 458 |
} |
| 459 |
func (f forbidden) Is(e error) bool { |
| 460 |
return IsForbidden(e) |
| 461 |
} |
| 462 |
func (b badGateway) Is(e error) bool { |
| 463 |
return IsBadGateway(e) |
| 464 |
} |
| 465 |
func (g gone) Is(e error) bool { |
| 466 |
return IsGone(e) |
| 467 |
} |
| 468 |
func (c conflict) Is(e error) bool { |
| 469 |
return IsConflict(e) |
| 470 |
} |
| 471 |
func (u unsupportedMediaType) Is(e error) bool { |
| 472 |
return IsUnsupportedMedia(e) |
| 473 |
} |
| 474 |
func (n notFound) Unwrap() error { |
| 475 |
return n.Err.Unwrap() |
| 476 |
} |
| 477 |
func (n notValid) Unwrap() error { |
| 478 |
return n.Err.Unwrap() |
| 479 |
} |
| 480 |
func (n notImplemented) Unwrap() error { |
| 481 |
return n.Err.Unwrap() |
| 482 |
} |
| 483 |
func (n notSupportedVersion) Unwrap() error { |
| 484 |
return n.Err.Unwrap() |
| 485 |
} |
| 486 |
func (b badRequest) Unwrap() error { |
| 487 |
return b.Err.Unwrap() |
| 488 |
} |
| 489 |
func (s serviceUnavailable) Unwrap() error { |
| 490 |
return s.Err.Unwrap() |
| 491 |
} |
| 492 |
func (t timeout) Unwrap() error { |
| 493 |
return t.Err.Unwrap() |
| 494 |
} |
| 495 |
func (u unauthorized) Unwrap() error { |
| 496 |
return u.Err.Unwrap() |
| 497 |
} |
| 498 |
func (m methodNotAllowed) Unwrap() error { |
| 499 |
return m.Err.Unwrap() |
| 500 |
} |
| 501 |
func (f forbidden) Unwrap() error { |
| 502 |
return f.Err.Unwrap() |
| 503 |
} |
| 504 |
func (b badGateway) Unwrap() error { |
| 505 |
return b.Err.Unwrap() |
| 506 |
} |
| 507 |
func (g gone) Unwrap() error { |
| 508 |
return g.Err.Unwrap() |
| 509 |
} |
| 510 |
func (u unsupportedMediaType) Unwrap() error { |
| 511 |
return u.Err.Unwrap() |
| 512 |
} |
| 513 |
func (c conflict) Unwrap() error { |
| 514 |
return c.Err.Unwrap() |
| 515 |
} |
| 516 |
|
| 517 |
|
| 518 |
|
| 519 |
|
| 520 |
|
| 521 |
|
| 522 |
func (n *notFound) As(err any) bool { |
| 523 |
switch x := err.(type) { |
| 524 |
case **notFound: |
| 525 |
*x = n |
| 526 |
case *notFound: |
| 527 |
*x = *n |
| 528 |
case *Err: |
| 529 |
return n.Err.As(x) |
| 530 |
default: |
| 531 |
return false |
| 532 |
} |
| 533 |
return true |
| 534 |
} |
| 535 |
|
| 536 |
|
| 537 |
|
| 538 |
|
| 539 |
|
| 540 |
|
| 541 |
func (n *notValid) As(err any) bool { |
| 542 |
switch x := err.(type) { |
| 543 |
case **notValid: |
| 544 |
*x = n |
| 545 |
case *notValid: |
| 546 |
*x = *n |
| 547 |
case *Err: |
| 548 |
return n.Err.As(x) |
| 549 |
default: |
| 550 |
return false |
| 551 |
} |
| 552 |
return true |
| 553 |
} |
| 554 |
|
| 555 |
|
| 556 |
|
| 557 |
|
| 558 |
|
| 559 |
|
| 560 |
func (n *notImplemented) As(err any) bool { |
| 561 |
switch x := err.(type) { |
| 562 |
case **notImplemented: |
| 563 |
*x = n |
| 564 |
case *notImplemented: |
| 565 |
*x = *n |
| 566 |
case *Err: |
| 567 |
return n.Err.As(x) |
| 568 |
default: |
| 569 |
return false |
| 570 |
} |
| 571 |
return true |
| 572 |
} |
| 573 |
|
| 574 |
|
| 575 |
|
| 576 |
|
| 577 |
|
| 578 |
|
| 579 |
func (n *notSupportedVersion) As(err any) bool { |
| 580 |
switch x := err.(type) { |
| 581 |
case **notSupportedVersion: |
| 582 |
*x = n |
| 583 |
case *notSupportedVersion: |
| 584 |
*x = *n |
| 585 |
case *Err: |
| 586 |
return n.Err.As(x) |
| 587 |
default: |
| 588 |
return false |
| 589 |
} |
| 590 |
return true |
| 591 |
} |
| 592 |
|
| 593 |
|
| 594 |
|
| 595 |
|
| 596 |
|
| 597 |
|
| 598 |
func (b *badRequest) As(err any) bool { |
| 599 |
switch x := err.(type) { |
| 600 |
case **badRequest: |
| 601 |
*x = b |
| 602 |
case *badRequest: |
| 603 |
*x = *b |
| 604 |
case *Err: |
| 605 |
return b.Err.As(x) |
| 606 |
default: |
| 607 |
return false |
| 608 |
} |
| 609 |
return true |
| 610 |
} |
| 611 |
|
| 612 |
func (s *serviceUnavailable) As(err any) bool { |
| 613 |
switch x := err.(type) { |
| 614 |
case **serviceUnavailable: |
| 615 |
*x = s |
| 616 |
case *serviceUnavailable: |
| 617 |
*x = *s |
| 618 |
case *Err: |
| 619 |
return s.Err.As(x) |
| 620 |
default: |
| 621 |
return false |
| 622 |
} |
| 623 |
return true |
| 624 |
} |
| 625 |
|
| 626 |
|
| 627 |
|
| 628 |
|
| 629 |
|
| 630 |
|
| 631 |
func (t *timeout) As(err any) bool { |
| 632 |
switch x := err.(type) { |
| 633 |
case **timeout: |
| 634 |
*x = t |
| 635 |
case *timeout: |
| 636 |
*x = *t |
| 637 |
case *Err: |
| 638 |
return t.Err.As(x) |
| 639 |
default: |
| 640 |
return false |
| 641 |
} |
| 642 |
return true |
| 643 |
} |
| 644 |
|
| 645 |
|
| 646 |
|
| 647 |
|
| 648 |
|
| 649 |
|
| 650 |
func (u *unauthorized) As(err any) bool { |
| 651 |
switch x := err.(type) { |
| 652 |
case **unauthorized: |
| 653 |
*x = u |
| 654 |
case *unauthorized: |
| 655 |
*x = *u |
| 656 |
case *Err: |
| 657 |
return u.Err.As(x) |
| 658 |
default: |
| 659 |
return false |
| 660 |
} |
| 661 |
return true |
| 662 |
} |
| 663 |
|
| 664 |
|
| 665 |
|
| 666 |
|
| 667 |
|
| 668 |
|
| 669 |
func (m *methodNotAllowed) As(err any) bool { |
| 670 |
switch x := err.(type) { |
| 671 |
case **methodNotAllowed: |
| 672 |
*x = m |
| 673 |
case *methodNotAllowed: |
| 674 |
*x = *m |
| 675 |
case *Err: |
| 676 |
return m.Err.As(x) |
| 677 |
default: |
| 678 |
return false |
| 679 |
} |
| 680 |
return true |
| 681 |
} |
| 682 |
|
| 683 |
|
| 684 |
|
| 685 |
|
| 686 |
|
| 687 |
|
| 688 |
func (f *forbidden) As(err any) bool { |
| 689 |
switch x := err.(type) { |
| 690 |
case **forbidden: |
| 691 |
*x = f |
| 692 |
case *forbidden: |
| 693 |
*x = *f |
| 694 |
case *Err: |
| 695 |
return f.Err.As(x) |
| 696 |
default: |
| 697 |
return false |
| 698 |
} |
| 699 |
return true |
| 700 |
} |
| 701 |
|
| 702 |
|
| 703 |
|
| 704 |
|
| 705 |
|
| 706 |
|
| 707 |
func (b *badGateway) As(err any) bool { |
| 708 |
switch x := err.(type) { |
| 709 |
case **badGateway: |
| 710 |
*x = b |
| 711 |
case *badGateway: |
| 712 |
*x = *b |
| 713 |
case *Err: |
| 714 |
return b.Err.As(x) |
| 715 |
default: |
| 716 |
return false |
| 717 |
} |
| 718 |
return true |
| 719 |
} |
| 720 |
|
| 721 |
|
| 722 |
|
| 723 |
|
| 724 |
|
| 725 |
|
| 726 |
func (g *gone) As(err any) bool { |
| 727 |
switch x := err.(type) { |
| 728 |
case **gone: |
| 729 |
*x = g |
| 730 |
case *gone: |
| 731 |
*x = *g |
| 732 |
case *Err: |
| 733 |
return g.Err.As(x) |
| 734 |
default: |
| 735 |
return false |
| 736 |
} |
| 737 |
return true |
| 738 |
} |
| 739 |
|
| 740 |
func (u *unsupportedMediaType) As(err any) bool { |
| 741 |
switch x := err.(type) { |
| 742 |
case **unsupportedMediaType: |
| 743 |
*x = u |
| 744 |
case *unsupportedMediaType: |
| 745 |
*x = *u |
| 746 |
case *Err: |
| 747 |
return u.Err.As(x) |
| 748 |
default: |
| 749 |
return false |
| 750 |
} |
| 751 |
return true |
| 752 |
} |
| 753 |
|
| 754 |
|
| 755 |
|
| 756 |
|
| 757 |
|
| 758 |
|
| 759 |
func (c *conflict) As(err any) bool { |
| 760 |
switch x := err.(type) { |
| 761 |
case **conflict: |
| 762 |
*x = c |
| 763 |
case *conflict: |
| 764 |
*x = *c |
| 765 |
case *Err: |
| 766 |
return c.Err.As(x) |
| 767 |
default: |
| 768 |
return false |
| 769 |
} |
| 770 |
return true |
| 771 |
} |
| 772 |
|
| 773 |
|
| 774 |
func (u *unauthorized) Challenge(c string) *unauthorized { |
| 775 |
u.challenge = c |
| 776 |
return u |
| 777 |
} |
| 778 |
|
| 779 |
|
| 780 |
func Challenge(err error) string { |
| 781 |
un := unauthorized{} |
| 782 |
if ok := As(err, &un); ok { |
| 783 |
return un.challenge |
| 784 |
} |
| 785 |
return "" |
| 786 |
} |
| 787 |
|
| 788 |
|
| 789 |
type ErrorHandlerFn func(http.ResponseWriter, *http.Request) error |
| 790 |
|
| 791 |
|
| 792 |
func (h ErrorHandlerFn) ServeHTTP(w http.ResponseWriter, r *http.Request) { |
| 793 |
var dat []byte |
| 794 |
var status int |
| 795 |
|
| 796 |
if err := h(w, r); err != nil { |
| 797 |
if IsRedirect(err) { |
| 798 |
w.Header().Set("Location", Location(err)) |
| 799 |
status = HttpStatus(err) |
| 800 |
} else { |
| 801 |
if status, dat = RenderErrors(r, err); status == 0 { |
| 802 |
status = http.StatusInternalServerError |
| 803 |
} |
| 804 |
w.Header().Set("Content-Type", "application/json") |
| 805 |
} |
| 806 |
} |
| 807 |
w.WriteHeader(status) |
| 808 |
w.Write(dat) |
| 809 |
} |
| 810 |
|
| 811 |
func Location(err error) string { |
| 812 |
r := new(redirect) |
| 813 |
if As(err, r) { |
| 814 |
return r.u |
| 815 |
} |
| 816 |
return "" |
| 817 |
} |
| 818 |
|
| 819 |
|
| 820 |
func HandleError(e error) ErrorHandlerFn { |
| 821 |
return func(w http.ResponseWriter, r *http.Request) error { |
| 822 |
return e |
| 823 |
} |
| 824 |
} |
| 825 |
|
| 826 |
|
| 827 |
var NotFound = ErrorHandlerFn(func(w http.ResponseWriter, r *http.Request) error { |
| 828 |
return NotFoundf("%s not found", r.URL.Path) |
| 829 |
}) |
| 830 |
|
| 831 |
type Http struct { |
| 832 |
Code int `jsonld:"status,omitempty"` |
| 833 |
Message string `jsonld:"message"` |
| 834 |
Trace StackTrace `jsonld:"trace,omitempty"` |
| 835 |
} |
| 836 |
|
| 837 |
func HttpErrors(err error) []Http { |
| 838 |
https := make([]Http, 0) |
| 839 |
|
| 840 |
withBacktrace := includeBacktrace.Load() |
| 841 |
load := func(err error) Http { |
| 842 |
var trace StackTrace |
| 843 |
var msg string |
| 844 |
if e, ok := err.(*Err); ok { |
| 845 |
msg = e.Error() |
| 846 |
if withBacktrace { |
| 847 |
trace = e.StackTrace() |
| 848 |
} |
| 849 |
} else { |
| 850 |
local := new(Err) |
| 851 |
if ok := As(err, local); ok { |
| 852 |
if withBacktrace { |
| 853 |
trace = local.StackTrace() |
| 854 |
} |
| 855 |
} |
| 856 |
msg = err.Error() |
| 857 |
} |
| 858 |
|
| 859 |
return Http{ |
| 860 |
Message: msg, |
| 861 |
Trace: trace, |
| 862 |
Code: HttpStatus(err), |
| 863 |
} |
| 864 |
} |
| 865 |
https = append(https, load(err)) |
| 866 |
for { |
| 867 |
if err = Unwrap(err); err != nil { |
| 868 |
https = append(https, load(err)) |
| 869 |
} else { |
| 870 |
break |
| 871 |
} |
| 872 |
} |
| 873 |
|
| 874 |
return https |
| 875 |
} |
| 876 |
|
| 877 |
func HttpStatus(e error) int { |
| 878 |
if IsRedirect(e) { |
| 879 |
r := new(redirect) |
| 880 |
if As(e, r) { |
| 881 |
return r.s |
| 882 |
} |
| 883 |
} |
| 884 |
if IsBadRequest(e) { |
| 885 |
return http.StatusBadRequest |
| 886 |
} |
| 887 |
if IsUnauthorized(e) { |
| 888 |
return http.StatusUnauthorized |
| 889 |
} |
| 890 |
|
| 891 |
if IsForbidden(e) { |
| 892 |
return http.StatusForbidden |
| 893 |
} |
| 894 |
if IsNotFound(e) { |
| 895 |
return http.StatusNotFound |
| 896 |
} |
| 897 |
if IsMethodNotAllowed(e) { |
| 898 |
return http.StatusMethodNotAllowed |
| 899 |
} |
| 900 |
if IsNotValid(e) { |
| 901 |
return http.StatusNotAcceptable |
| 902 |
} |
| 903 |
|
| 904 |
|
| 905 |
if IsConflict(e) { |
| 906 |
return http.StatusConflict |
| 907 |
} |
| 908 |
if IsGone(e) { |
| 909 |
return http.StatusGone |
| 910 |
} |
| 911 |
|
| 912 |
|
| 913 |
|
| 914 |
|
| 915 |
if IsUnsupportedMedia(e) { |
| 916 |
return http.StatusUnsupportedMediaType |
| 917 |
} |
| 918 |
|
| 919 |
|
| 920 |
|
| 921 |
|
| 922 |
|
| 923 |
|
| 924 |
|
| 925 |
|
| 926 |
|
| 927 |
|
| 928 |
|
| 929 |
|
| 930 |
|
| 931 |
if IsNotImplemented(e) { |
| 932 |
return http.StatusNotImplemented |
| 933 |
} |
| 934 |
if IsBadGateway(e) { |
| 935 |
return http.StatusBadGateway |
| 936 |
} |
| 937 |
if IsServiceUnavailable(e) { |
| 938 |
return http.StatusServiceUnavailable |
| 939 |
} |
| 940 |
|
| 941 |
if IsNotSupported(e) { |
| 942 |
return http.StatusHTTPVersionNotSupported |
| 943 |
} |
| 944 |
|
| 945 |
if IsTimeout(e) { |
| 946 |
return http.StatusGatewayTimeout |
| 947 |
} |
| 948 |
|
| 949 |
return 0 |
| 950 |
} |
| 951 |
|
| 952 |
func errorFromStatus(status int) Error { |
| 953 |
switch status { |
| 954 |
case http.StatusBadRequest: |
| 955 |
return new(badRequest) |
| 956 |
case http.StatusUnauthorized: |
| 957 |
return new(unauthorized) |
| 958 |
|
| 959 |
case http.StatusForbidden: |
| 960 |
return new(forbidden) |
| 961 |
case http.StatusNotFound: |
| 962 |
return new(notFound) |
| 963 |
case http.StatusMethodNotAllowed: |
| 964 |
return new(methodNotAllowed) |
| 965 |
case http.StatusNotAcceptable: |
| 966 |
return new(notValid) |
| 967 |
|
| 968 |
|
| 969 |
case http.StatusConflict: |
| 970 |
return new(conflict) |
| 971 |
case http.StatusGone: |
| 972 |
return new(gone) |
| 973 |
|
| 974 |
|
| 975 |
|
| 976 |
|
| 977 |
case http.StatusUnsupportedMediaType: |
| 978 |
return new(unsupportedMediaType) |
| 979 |
|
| 980 |
|
| 981 |
|
| 982 |
|
| 983 |
|
| 984 |
|
| 985 |
|
| 986 |
|
| 987 |
|
| 988 |
|
| 989 |
|
| 990 |
|
| 991 |
case http.StatusNotImplemented: |
| 992 |
return new(notImplemented) |
| 993 |
case http.StatusBadGateway: |
| 994 |
return new(badGateway) |
| 995 |
case http.StatusServiceUnavailable: |
| 996 |
return new(serviceUnavailable) |
| 997 |
case http.StatusHTTPVersionNotSupported: |
| 998 |
return new(notSupportedVersion) |
| 999 |
case http.StatusGatewayTimeout: |
| 1000 |
return new(badGateway) |
| 1001 |
case http.StatusInternalServerError: |
| 1002 |
fallthrough |
| 1003 |
default: |
| 1004 |
return new(Err) |
| 1005 |
} |
| 1006 |
} |
| 1007 |
|
| 1008 |
|
| 1009 |
func ctxt(r *http.Request) jsonld.Context { |
| 1010 |
scheme := "http" |
| 1011 |
if r.TLS != nil { |
| 1012 |
scheme = "https" |
| 1013 |
} |
| 1014 |
return jsonld.Context{ |
| 1015 |
jsonld.ContextElement{ |
| 1016 |
Term: "errors", |
| 1017 |
IRI: jsonld.IRI(fmt.Sprintf("%s://%s/ns#errors", scheme, r.Host)), |
| 1018 |
}, |
| 1019 |
} |
| 1020 |
} |
| 1021 |
|
| 1022 |
|
| 1023 |
func RenderErrors(r *http.Request, errs ...error) (int, []byte) { |
| 1024 |
errMap := make([]Http, 0) |
| 1025 |
var status int |
| 1026 |
for _, err := range errs { |
| 1027 |
more := HttpErrors(err) |
| 1028 |
errMap = append(errMap, more...) |
| 1029 |
status = HttpStatus(err) |
| 1030 |
} |
| 1031 |
var dat []byte |
| 1032 |
var err error |
| 1033 |
|
| 1034 |
m := struct { |
| 1035 |
Errors []Http `jsonld:"errors"` |
| 1036 |
}{Errors: errMap} |
| 1037 |
if dat, err = jsonld.WithContext(ctxt(r)).Marshal(m); err != nil { |
| 1038 |
return http.StatusInternalServerError, dat |
| 1039 |
} |
| 1040 |
return status, dat |
| 1041 |
} |