Skip to content

Commit

Permalink
Use more idiomatic Go to print error messages
Browse files Browse the repository at this point in the history
  • Loading branch information
jamlo committed Oct 23, 2024
1 parent 17fb044 commit d42d027
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 20 deletions.
24 changes: 12 additions & 12 deletions pkg/docker/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,62 +48,62 @@ func NewDockerError(err error) (bacErr bacerrors.Error) {
case errdefs.IsNotFound(err):
return handleNotFoundError(err)
case errdefs.IsConflict(err):
return bacerrors.New("%s", err.Error()).
return bacerrors.New("%s", err).
WithCode(Conflict).
WithHTTPStatusCode(http.StatusConflict)
case errdefs.IsUnauthorized(err):
return bacerrors.New("%s", err.Error()).
return bacerrors.New("%s", err).
WithCode(Unauthorized).
WithHTTPStatusCode(http.StatusUnauthorized).
WithHint("Ensure you have the necessary permissions and that your credentials are correct. " +
"You may need to log in to Docker again.")
case errdefs.IsForbidden(err):
return bacerrors.New("%s", err.Error()).
return bacerrors.New("%s", err).
WithCode(Forbidden).
WithHTTPStatusCode(http.StatusForbidden).
WithHint(fmt.Sprintf("You don't have permission to perform this action. "+
"Supply the node with valid Docker login credentials using the %s and %s environment variables",
config_legacy.DockerUsernameEnvVar, config_legacy.DockerPasswordEnvVar))
case errdefs.IsDataLoss(err):
return bacerrors.New("%s", err.Error()).
return bacerrors.New("%s", err).
WithCode(DataLoss).
WithHTTPStatusCode(http.StatusInternalServerError).
WithFailsExecution()
case errdefs.IsDeadline(err):
return bacerrors.New("%s", err.Error()).
return bacerrors.New("%s", err).
WithCode(Deadline).
WithHTTPStatusCode(http.StatusGatewayTimeout).
WithHint("The operation timed out. This could be due to network issues or high system load. " +
"Try again later or check your network connection.").
WithRetryable()
case errdefs.IsCancelled(err):
return bacerrors.New("%s", err.Error()).
return bacerrors.New("%s", err).
WithCode(Cancelled).
WithHTTPStatusCode(http.StatusRequestTimeout).
WithHint("The operation was cancelled. " +
"This is often due to user intervention or a competing operation.")
case errdefs.IsUnavailable(err):
return bacerrors.New("%s", err.Error()).
return bacerrors.New("%s", err).
WithCode(Unavailable).
WithHTTPStatusCode(http.StatusServiceUnavailable).
WithHint("The Docker daemon or a required service is unavailable. " +
"Check if the Docker daemon is running and healthy.").
WithRetryable()
case errdefs.IsSystem(err):
return bacerrors.New("%s", err.Error()).
return bacerrors.New("%s", err).
WithCode(SystemError).
WithHTTPStatusCode(http.StatusInternalServerError).
WithHint("An internal system error occurred. This could be due to resource constraints. " +
"Check system resources and Docker logs for more information.").
WithFailsExecution()
case errdefs.IsNotImplemented(err):
return bacerrors.New("%s", err.Error()).
return bacerrors.New("%s", err).
WithCode(NotImplemented).
WithHTTPStatusCode(http.StatusNotImplemented).
WithHint("This feature is not implemented in your version of Docker. " +
"Check Docker documentation for feature availability and consider upgrading if necessary.")
default:
return bacerrors.New("%s", err.Error()).
return bacerrors.New("%s", err).
WithCode(UnknownError).
WithHTTPStatusCode(http.StatusInternalServerError)
}
Expand Down Expand Up @@ -144,11 +144,11 @@ func NewCustomDockerError(code bacerrors.ErrorCode, message string) bacerrors.Er
func handleNotFoundError(err error) bacerrors.Error {
errorLower := strings.ToLower(err.Error())
if strings.Contains(errorLower, "no such container") {
return bacerrors.New("%s", err.Error()).
return bacerrors.New("%s", err).
WithCode(ContainerNotFound).
WithHTTPStatusCode(http.StatusNotFound)
}
return bacerrors.New("%s", err.Error()).
return bacerrors.New("%s", err).
WithCode(NotFound).
WithHTTPStatusCode(http.StatusNotFound)
}
16 changes: 8 additions & 8 deletions pkg/jobstore/boltdb/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,38 +24,38 @@ const (
func NewBoltDBError(err error) bacerrors.Error {
switch {
case errors.Is(err, bbolt.ErrBucketNotFound):
return bacerrors.New("%s", err.Error()).
return bacerrors.New("%s", err).
WithCode(BoltDBBucketNotFound).
WithHTTPStatusCode(http.StatusNotFound).
WithComponent(BoltDBComponent)
case errors.Is(err, bbolt.ErrBucketExists):
return bacerrors.New("%s", err.Error()).
return bacerrors.New("%s", err).
WithCode(BoltDBBucketExists).
WithHTTPStatusCode(http.StatusConflict).
WithComponent(BoltDBComponent)
case errors.Is(err, bbolt.ErrTxNotWritable):
return bacerrors.New("%s", err.Error()).
return bacerrors.New("%s", err).
WithCode(BoltDBTxNotWritable).
WithHTTPStatusCode(http.StatusInternalServerError).
WithComponent(BoltDBComponent)
case errors.Is(err, bbolt.ErrIncompatibleValue):
return bacerrors.New("%s", err.Error()).
return bacerrors.New("%s", err).
WithCode(BoltDBIncompatibleValue).
WithComponent(BoltDBComponent)
case errors.Is(err, bbolt.ErrKeyRequired):
return bacerrors.New("%s", err.Error()).
return bacerrors.New("%s", err).
WithCode(BoltDBKeyRequired).
WithComponent(BoltDBComponent)
case errors.Is(err, bbolt.ErrKeyTooLarge):
return bacerrors.New("%s", err.Error()).
return bacerrors.New("%s", err).
WithCode(BoltDBKeyTooLarge).
WithComponent(BoltDBComponent)
case errors.Is(err, bbolt.ErrValueTooLarge):
return bacerrors.New("%s", err.Error()).
return bacerrors.New("%s", err).
WithCode(BoltDBValueTooLarge).
WithComponent(BoltDBComponent)
default:
return bacerrors.New("%s", err.Error()).
return bacerrors.New("%s", err).
WithCode(bacerrors.BadRequestError).
WithComponent(BoltDBComponent)
}
Expand Down

0 comments on commit d42d027

Please sign in to comment.