Skip to content
cloudemu

Error Handling

Canonical error codes and helper functions in cloudemu

Match any cloudemu error against the 11 canonical codes and their helper functions. Import the errors package:

import cerrors "github.com/stackshy/cloudemu/v2/errors"

Error codes#

CodeDescription
OKNo error
NotFoundResource does not exist
AlreadyExistsResource already exists
InvalidArgumentInvalid input parameter
FailedPreconditionOperation rejected due to current state (e.g., invalid state transition)
PermissionDeniedIAM policy denies the action
ThrottledRate limit exceeded
InternalInternal error
UnimplementedOperation not implemented
ResourceExhaustedResource quota exceeded
UnavailableService temporarily unavailable

Checking error types#

Use the helper functions to check specific error types:

_, err := aws.S3.GetObject(ctx, "bucket", "missing-key")
if cerrors.IsNotFound(err) {
    // Handle missing resource
}

err = aws.S3.CreateBucket(ctx, "existing-bucket")
if cerrors.IsAlreadyExists(err) {
    // Bucket already exists
}

Available helpers#

cerrors.IsNotFound(err) bool
cerrors.IsAlreadyExists(err) bool
cerrors.IsThrottled(err) bool
cerrors.IsInvalidArgument(err) bool
cerrors.IsFailedPrecondition(err) bool
cerrors.IsPermissionDenied(err) bool

Extracting error codes#

For error codes without dedicated helpers, use GetCode:

code := cerrors.GetCode(err)

switch code {
case cerrors.NotFound:
    // handle not found
case cerrors.Throttled:
    // handle rate limiting
case cerrors.ResourceExhausted:
    // handle quota exceeded
default:
    // handle other errors
}

GetCode returns:

  • OK for nil errors
  • The cloudemu error code for *cerrors.Error values
  • Internal for any other error type

Error format#

Error messages follow the pattern "Code: message":

err := cerrors.New(cerrors.NotFound, "bucket 'my-bucket' not found")
fmt.Println(err) // "NotFound: bucket 'my-bucket' not found"

err = cerrors.Newf(cerrors.InvalidArgument, "key %q is empty", "")
fmt.Println(err) // "InvalidArgument: key \"\" is empty"

On this page

On this page