File: http_helpers.go

package info (click to toggle)
docker.io 28.5.2%2Bdfsg1-1
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 69,048 kB
  • sloc: sh: 5,867; makefile: 863; ansic: 184; python: 162; asm: 159
file content (49 lines) | stat: -rw-r--r-- 1,303 bytes parent folder | download | duplicates (2)
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
package errdefs

import (
	"net/http"
)

// FromStatusCode creates an errdef error, based on the provided HTTP status-code
//
// Deprecated: Use [cerrdefs.ToNative] instead
func FromStatusCode(err error, statusCode int) error {
	if err == nil {
		return nil
	}
	switch statusCode {
	case http.StatusNotFound:
		return NotFound(err)
	case http.StatusBadRequest:
		return InvalidParameter(err)
	case http.StatusConflict:
		return Conflict(err)
	case http.StatusUnauthorized:
		return Unauthorized(err)
	case http.StatusServiceUnavailable:
		return Unavailable(err)
	case http.StatusForbidden:
		return Forbidden(err)
	case http.StatusNotModified:
		return NotModified(err)
	case http.StatusNotImplemented:
		return NotImplemented(err)
	case http.StatusInternalServerError:
		if IsCancelled(err) || IsSystem(err) || IsUnknown(err) || IsDataLoss(err) || IsDeadline(err) {
			return err
		}
		return System(err)
	default:
		switch {
		case statusCode >= http.StatusOK && statusCode < http.StatusBadRequest:
			// it's a client error
			return err
		case statusCode >= http.StatusBadRequest && statusCode < http.StatusInternalServerError:
			return InvalidParameter(err)
		case statusCode >= http.StatusInternalServerError && statusCode < 600:
			return System(err)
		default:
			return Unknown(err)
		}
	}
}