pbErrors

package
v0.0.0-...-c0b8c70 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 23, 2025 License: MIT Imports: 8 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// Validation error prefix
	ErrValidationPrefix = "failed to validate request"

	// Common validation errors
	ErrEmailAlreadyExists        = "email already exists"
	ErrInvalidResourceName       = "invalid resource name"
	ErrUserNotFound              = "user not found"
	ErrIncorrectCredentials      = "incorrect email or password"
	ErrUserNotActive             = "user is not active"
	ErrTooManyValidationRequests = "too many validation requests"
	ErrSessionNotFound           = "session not found"
	ErrSessionBlocked            = "session is blocked"
)

Error message constants

Variables

This section is empty.

Functions

func AlreadyExistsError

func AlreadyExistsError(message string, field string) error

AlreadyExistsError creates a gRPC AlreadyExists error

func ConvertProtoValidateError

func ConvertProtoValidateError(err error) error

ConvertProtoValidateError converts a protovalidate error to a gRPC InvalidArgumentError

func ExtractFieldViolations

func ExtractFieldViolations(err error) []*errdetails.BadRequest_FieldViolation

ExtractFieldViolations extracts field violations from an error

func FailedPreconditionError

func FailedPreconditionError(message string) error

FailedPreconditionError creates a gRPC FailedPrecondition error

func FieldViolation

func FieldViolation(field string, err error) *errdetails.BadRequest_FieldViolation

FieldViolation creates a field violation for gRPC error details

func FormatValidationError

func FormatValidationError(err error) error

FormatValidationError formats a validation error with the standard prefix

func GetFieldViolationMessage

func GetFieldViolationMessage(err error, field string) string

GetFieldViolationMessage gets the message for a specific field violation

func HasFieldViolation

func HasFieldViolation(err error, field string) bool

HasFieldViolation checks if an error has a field violation for a specific field

func InternalError

func InternalError(message string, err error) error

InternalError creates a gRPC Internal error

func InvalidArgumentError

func InvalidArgumentError(violations []*errdetails.BadRequest_FieldViolation) error

InvalidArgumentError creates a gRPC InvalidArgument error with field violations

func IsInternalError

func IsInternalError(err error) bool

IsInternalError checks if an error is an internal error

func IsInvalidArgumentError

func IsInvalidArgumentError(err error) bool

IsInvalidArgumentError checks if an error is an invalid argument error

func IsNotFoundError

func IsNotFoundError(err error) bool

IsNotFoundError checks if an error is a not found error

func IsPermissionDeniedError

func IsPermissionDeniedError(err error) bool

IsPermissionDeniedError checks if an error is a permission denied error

func IsUnauthenticatedError

func IsUnauthenticatedError(err error) bool

IsUnauthenticatedError checks if an error is an unauthenticated error

func IsValidationError

func IsValidationError(err error) bool

IsValidationError checks if an error is a validation error

func NewFieldValidationError

func NewFieldValidationError(field, message string) error

NewFieldValidationError creates a new field validation error with the standard format

func NewInternalError

func NewInternalError(message string, err error) error

NewInternalError creates a new internal error with the standard gRPC status

func NewNotFoundError

func NewNotFoundError(message string) error

NewNotFoundError creates a new not found error with the standard gRPC status

func NewUnauthenticatedError

func NewUnauthenticatedError(message string) error

NewUnauthenticatedError creates a new unauthenticated error with the standard gRPC status

func NewValidationError

func NewValidationError(message string) error

NewValidationError creates a new validation error with the standard prefix

func NotFoundError

func NotFoundError(message string) error

NotFoundError creates a gRPC NotFound error

func PermissionDeniedError

func PermissionDeniedError(message string) error

PermissionDeniedError creates a gRPC PermissionDenied error

func StandardConflictError

func StandardConflictError(resource, reason string) error

func StandardForbiddenError

func StandardForbiddenError(message string) error

func StandardInternalServerError

func StandardInternalServerError(message string) error

func StandardNotFoundError

func StandardNotFoundError(resource string) error

Common error builders for consistent error messages

func StandardUnauthorizedError

func StandardUnauthorizedError(message string) error

func UnauthenticatedError

func UnauthenticatedError(message string) error

UnauthenticatedError creates a gRPC Unauthenticated error

Types

type ErrorParser

type ErrorParser struct{}

ErrorParser provides utilities for parsing and converting different error types

func NewErrorParser

func NewErrorParser() *ErrorParser

NewErrorParser creates a new error parser instance

func (*ErrorParser) FromFormData

func (p *ErrorParser) FromFormData(fieldErrors map[string][]string, globalMessage string) *StandardError

FromFormData creates a validation error from form data This is useful when validating form submissions on the server side

func (*ErrorParser) ParseBusinessLogicError

func (p *ErrorParser) ParseBusinessLogicError(errorType ErrorType, message string, field string) *StandardError

ParseBusinessLogicError creates a business logic error

func (*ErrorParser) ParseConnectError

func (p *ErrorParser) ParseConnectError(err error) *StandardError

ParseConnectError converts Connect-RPC errors to StandardError

func (*ErrorParser) ParseProtoValidationError

func (p *ErrorParser) ParseProtoValidationError(err error) *StandardError

ParseProtoValidationError converts protovalidate errors to StandardError

func (*ErrorParser) ToFormErrorResponse

func (p *ErrorParser) ToFormErrorResponse(err *StandardError) *FormErrorResponse

ToFormErrorResponse converts StandardError to a form-friendly response

func (*ErrorParser) ValidateEmail

func (p *ErrorParser) ValidateEmail(email string, fieldName string) []string

ValidateEmail checks if an email is valid

func (*ErrorParser) ValidateLength

func (p *ErrorParser) ValidateLength(value string, fieldName string, min, max int) []string

ValidateLength checks string length constraints

func (*ErrorParser) ValidateRange

func (p *ErrorParser) ValidateRange(value int, fieldName string, min, max int) []string

ValidateRange checks numeric range constraints

func (*ErrorParser) ValidateRequired

func (p *ErrorParser) ValidateRequired(value string, fieldName string) []string

ValidateRequired checks if a field is required and not empty

type ErrorType

type ErrorType string

ErrorType represents the category of error for consistent handling

const (
	// Field-level errors that should be mapped to specific form fields
	ErrorTypeValidation ErrorType = "VALIDATION_ERROR"

	// Global errors that should be shown as toasts or general messages
	ErrorTypeNotFound     ErrorType = "NOT_FOUND"
	ErrorTypeUnauthorized ErrorType = "UNAUTHORIZED"
	ErrorTypeForbidden    ErrorType = "FORBIDDEN"
	ErrorTypeConflict     ErrorType = "CONFLICT"
	ErrorTypeInternal     ErrorType = "INTERNAL_ERROR"
	ErrorTypeRateLimit    ErrorType = "RATE_LIMIT"
	ErrorTypeUnavailable  ErrorType = "UNAVAILABLE"
)

type FormErrorResponse

type FormErrorResponse struct {
	Success     bool                `json:"success"`
	Message     string              `json:"message,omitempty"`
	FieldErrors map[string][]string `json:"field_errors,omitempty"`
	GlobalError string              `json:"global_error,omitempty"`
	ErrorType   string              `json:"error_type,omitempty"`
}

FormErrorResponse represents the structure for frontend error handling

type StandardError

type StandardError struct {
	Type        ErrorType
	Message     string
	Fields      map[string][]string // Field-specific error messages
	GlobalError string              // Global error message for toasts
	Details     map[string]string   // Additional error details
}

StandardError represents a standardized error structure

func FromConnectError

func FromConnectError(err error) *StandardError

FromConnectError converts a Connect-RPC error to StandardError

func NewGlobalError

func NewGlobalError(errorType ErrorType, message string) *StandardError

NewGlobalError creates a new global error (for toast notifications)

func NewStandardValidationError

func NewStandardValidationError(message string, fieldErrors map[string][]string) *StandardError

NewStandardValidationError creates a new validation error with field mappings

func (*StandardError) AddDetail

func (e *StandardError) AddDetail(key, value string)

AddDetail adds additional error details

func (*StandardError) AddFieldError

func (e *StandardError) AddFieldError(field string, message string)

AddFieldError adds a field-specific error

func (*StandardError) HasFieldErrors

func (e *StandardError) HasFieldErrors() bool

HasFieldErrors returns true if there are field-specific errors

func (*StandardError) HasGlobalError

func (e *StandardError) HasGlobalError() bool

HasGlobalError returns true if there's a global error message

func (*StandardError) ToConnectError

func (e *StandardError) ToConnectError() error

ToConnectError converts StandardError to a Connect-RPC error

type ValidationErrorBuilder

type ValidationErrorBuilder struct {
	// contains filtered or unexported fields
}

ValidationErrorBuilder helps build validation errors consistently

func BuildValidationError

func BuildValidationError() *ValidationErrorBuilder

BuildValidationError is a convenience function for building validation errors

func NewValidationErrorBuilder

func NewValidationErrorBuilder(message string) *ValidationErrorBuilder

NewValidationErrorBuilder creates a new validation error builder

func (*ValidationErrorBuilder) AddField

func (b *ValidationErrorBuilder) AddField(field, message string) *ValidationErrorBuilder

AddField adds a field error to the builder

func (*ValidationErrorBuilder) Build

Build creates the final StandardError

func (*ValidationErrorBuilder) BuildConnectError

func (b *ValidationErrorBuilder) BuildConnectError() error

BuildConnectError creates a Connect-RPC error directly

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL