cloudemu

Error Handling

Canonical error codes and helper functions in cloudemu

Error Handling

All cloudemu operations return errors with canonical codes. Import the errors package:

import cerrors "github.com/stackshy/cloudemu/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