gonest
Request Pipeline

Exceptions & Panic Recovery

HttpException, built-in exceptions, and the panic-recovery contract

gonest.HttpException is the base type every gonest exception embeds.

gonest.NewHttpException().
  SetStatus(http.StatusBadRequest).
  SetName("FooError").
  SetMessage("lorem ipsum").
  SetDetails(details)

Any type embedding HttpException structurally satisfies the Exception interface — the recovery layer uses a type-assertion, never a closed type-switch.

Built-in exceptions

ExceptionStatus
NotFoundException404
BadRequestException400
ConflictException409
UnauthorizedException401
ForbiddenException403

Each has a New*Exception(details any) constructor.

Custom exceptions

Embed gonest.HttpException exactly like a built-in:

type FooError struct{ gonest.HttpException }

func NewFooError(details any) *FooError {
  return &FooError{
    HttpException: gonest.NewHttpException().
      SetStatus(http.StatusBadRequest).
      SetName("FooError").
      SetMessage("lorem ipsum").
      SetDetails(details),
  }
}

Panic → response contract

Any panic with a value satisfying Exception becomes {"name": ..., "message": ..., "details": ...} JSON at that exception's own status code. Any other panic (nil pointer, index out of range, etc.) becomes a generic 500 with zero leaked internal detail — the process never crashes.

HTTP status constants

gonest.HttpStatus* re-exports all 63 standard net/http.StatusXxx constants under the gonest namespace (e.g. gonest.HttpStatusNotFound), so handler code never needs to import net/http just for status codes.

Next steps

On this page