Skip to content

Commit

Permalink
Merge pull request moby#48100 from thaJeztah/errdefs_fromstatuscode
Browse files Browse the repository at this point in the history
errdefs: FromStatusCode(): use early returns
  • Loading branch information
AkihiroSuda authored Jul 1, 2024
2 parents 4612773 + 625c795 commit d91ae49
Showing 1 changed file with 15 additions and 14 deletions.
29 changes: 15 additions & 14 deletions errdefs/http_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,36 +11,37 @@ func FromStatusCode(err error, statusCode int) error {
}
switch statusCode {
case http.StatusNotFound:
err = NotFound(err)
return NotFound(err)
case http.StatusBadRequest:
err = InvalidParameter(err)
return InvalidParameter(err)
case http.StatusConflict:
err = Conflict(err)
return Conflict(err)
case http.StatusUnauthorized:
err = Unauthorized(err)
return Unauthorized(err)
case http.StatusServiceUnavailable:
err = Unavailable(err)
return Unavailable(err)
case http.StatusForbidden:
err = Forbidden(err)
return Forbidden(err)
case http.StatusNotModified:
err = NotModified(err)
return NotModified(err)
case http.StatusNotImplemented:
err = NotImplemented(err)
return NotImplemented(err)
case http.StatusInternalServerError:
if !IsSystem(err) && !IsUnknown(err) && !IsDataLoss(err) && !IsDeadline(err) && !IsCancelled(err) {
err = System(err)
if IsCancelled(err) || IsSystem(err) || IsUnknown(err) || IsDataLoss(err) || IsDeadline(err) {
return err
}
return System(err)
default:
switch {
case statusCode >= 200 && statusCode < 400:
// it's a client error
return err
case statusCode >= 400 && statusCode < 500:
err = InvalidParameter(err)
return InvalidParameter(err)
case statusCode >= 500 && statusCode < 600:
err = System(err)
return System(err)
default:
err = Unknown(err)
return Unknown(err)
}
}
return err
}

0 comments on commit d91ae49

Please sign in to comment.