2023-12-21 16:04:04 +00:00
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2024-06-10 18:03:25 +00:00
|
|
|
errorValidationNotEmpty = newServiceError(illegalArgument, errors.New("assert: empty values are not allowed"))
|
|
|
|
errorValidationNotBlank = newServiceError(illegalArgument, errors.New("assert: blank values are not allowed"))
|
|
|
|
errorValidationPageGreaterZero = newServiceError(illegalArgument, errors.New("assert: page has to be greater 0"))
|
|
|
|
errorValidationPageSizeGreaterZero = newServiceError(illegalArgument, errors.New("assert: pageSize has to be greater 0"))
|
|
|
|
errorValidationLimitGreaterZero = newServiceError(illegalArgument, errors.New("assert: limit has to be greater 0"))
|
|
|
|
errorValidationSizeGreaterZero = newServiceError(illegalArgument, errors.New("assert: size has to be greater 0"))
|
|
|
|
errorValidationMaxRetriesGreaterZero = newServiceError(illegalArgument, errors.New("assert: max retries has to be greater 0"))
|
2023-12-21 16:04:04 +00:00
|
|
|
|
2024-06-10 18:03:25 +00:00
|
|
|
errorResourceNotFound = newServiceError(notFound, errors.New("resource not found"))
|
|
|
|
errorResourceAccessDenied = newServiceError(forbidden, errors.New("resource access denied"))
|
2023-12-21 16:04:04 +00:00
|
|
|
|
|
|
|
errorDatabaseRowsExpected = newServiceDatabaseError(errors.New("action failed, expected affected rows, but got none"))
|
|
|
|
)
|
|
|
|
|
2024-06-10 18:03:25 +00:00
|
|
|
type errorCode string
|
2023-12-21 16:04:04 +00:00
|
|
|
|
|
|
|
const (
|
2024-06-10 18:03:25 +00:00
|
|
|
illegalArgument errorCode = "IllegalArgument"
|
|
|
|
unauthorized errorCode = "Unauthorized"
|
|
|
|
forbidden errorCode = "Forbidden"
|
|
|
|
notFound errorCode = "NotFound"
|
|
|
|
methodNotAllowed errorCode = "MethodNotAllowed"
|
|
|
|
conflict errorCode = "Conflict"
|
|
|
|
general errorCode = "General"
|
2023-12-21 16:04:04 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// newServiceError returns an error that formats as the given text and aligns with builtin error
|
2024-06-10 18:03:25 +00:00
|
|
|
func newServiceError(status errorCode, err error) error {
|
2023-12-21 16:04:04 +00:00
|
|
|
return &serviceError{status, fmt.Errorf("service error (%v): %w", status, err)}
|
|
|
|
}
|
|
|
|
|
|
|
|
// newServiceDatabaseError returns an error that formats as the given text and aligns with builtin error
|
|
|
|
func newServiceDatabaseError(error error) error {
|
2024-06-10 18:03:25 +00:00
|
|
|
return newServiceError(general, fmt.Errorf("database error: %w", error))
|
2023-12-21 16:04:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type serviceError struct {
|
2024-06-10 18:03:25 +00:00
|
|
|
Status errorCode
|
2023-12-21 16:04:04 +00:00
|
|
|
Cause error
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *serviceError) Error() string {
|
|
|
|
return fmt.Sprintf("%v", e.Cause)
|
|
|
|
}
|