Packages

errs

Typed HTTP errors with status codes and machine-readable error codes. Safe to use as global sentinels — value types prevent external mutation.


Constructors

import "github.com/BounkhongDev/bkgo/errs"

return errs.NotFound("user not found")        // 404 NOT_FOUND
return errs.BadRequest("invalid email")       // 400 BAD_REQUEST
return errs.Unauthorized("login required")    // 401 UNAUTHORIZED
return errs.Forbidden("access denied")        // 403 FORBIDDEN
return errs.Conflict("email already taken")   // 409 CONFLICT
return errs.Internal("db connection lost")    // 500 INTERNAL_ERROR
return errs.Unprocessable("validation failed") // 422 UNPROCESSABLE

Type checking

// In your handler
user, err := uc.FindByID(ctx, id)
if err != nil {
    if ae, ok := errs.IsAppError(err); ok {
        return c.Status(ae.Status).JSON(
            response.Error(ae.Code, i18n.Translate(locale, ae.Code)),
        )
    }
    // unexpected error — log internally, return generic message
    slog.Error("find user", "error", err)
    return c.Status(500).JSON(
        response.Error("INTERNAL_ERROR", i18n.Translate(locale, "INTERNAL_ERROR")),
    )
}

AppError struct

type AppError struct {
    Status  int    // HTTP status code
    Code    string // machine-readable code (used for i18n lookup)
    Message string // human-readable message
}

// Implements error interface
func (e AppError) Error() string { return e.Message }