Reviewer · Error Handling
Classify and preserve errors
Errors that cross an API boundary must be wrapped in the correct `errdefs` type (`NotFound`, `InvalidParameter`, `Forbidden`, etc.) so the HTTP layer returns the right status code instead of defaulting to 500. A plain `fmt.Errorf`/`errors.New` on a user-facing path (missing checkpoint directory, invalid checkpoint/container name, reserved network name,...
Errors that cross an API boundary must be wrapped in the correct errdefs type (NotFound, InvalidParameter, Forbidden, etc.) so the HTTP layer returns the right status code instead of defaulting to 500. A plain fmt.Errorf/errors.New on a user-facing path (missing checkpoint directory, invalid checkpoint/container name, reserved network name, etc.) is a bug even if the message is otherwise correct:
// Bad: untyped error -> HTTP 500, even though this is a client mistake
if err != nil {
return fmt.Errorf("checkpoint directory %q does not exist for container %s", dir, name)
}
// Good: classify the failure so the API responds correctly
if err != nil {
if errors.Is(err, fs.ErrNotExist) {
return errdefs.NotFound(fmt.Errorf("checkpoint directory %q does not exist for container %s: %w", dir, name, err))
}
return fmt.Errorf("failed to stat checkpoint directory %q: %w", dir, err)
}
When wrapping or converting an error, never discard the original cause: use %w (or errors.Join when combining independent failures) so the root error remains inspectable with errors.Is/errors.As, and so operators keep the diagnostic detail (e.g. EPERM vs ENOENT). Avoid building a new, generic error from an existing error value when the original can simply be returned or wrapped.
Finally, be deliberate about whether a failure is recoverable: don’t let errors on critical paths (startup, initialization, semaphore/resource acquisition) be silently logged and swallowed, as that can leave the system in a partially-initialized state — fail loudly and propagate instead. For genuinely best-effort or non-critical paths (e.g. populating a non-critical field, legacy data fallbacks), it’s fine to log-and-continue, but that choice should be explicit, consistent with similar code elsewhere in the codebase, and documented with a short comment explaining why the error is not fatal.