Skip to content

Commit

Permalink
ServiceError
Browse files Browse the repository at this point in the history
  • Loading branch information
pieceowater committed Oct 1, 2024
1 parent 7c9a45e commit 8a37257
Showing 1 changed file with 19 additions and 9 deletions.
28 changes: 19 additions & 9 deletions internal/tools/formats/errors/error.go
Original file line number Diff line number Diff line change
@@ -1,26 +1,36 @@
package errors

import (
"net/http"
)

// ServiceError represents a custom error used throughout the application.
type ServiceError struct {
Message string // The error message
StatusCode int `json:"statusCode"`
Message string `json:"message"`
}

// Error returns the error message, making ServiceError comply with the error interface.
func (e *ServiceError) Error() string {
return e.Message
}

// GetError returns a map representing the error name and message.
func (e *ServiceError) GetError() map[string]string {
return map[string]string{
"name": "ServiceError",
"message": e.Message,
// GetError returns a map representing the error status code and message.
func (e *ServiceError) GetError() map[string]interface{} {
return map[string]any{
"message": e.Message,
"statusCode": e.StatusCode,
}
}

// NewServiceError creates a new ServiceError instance with the given message.
func NewServiceError(message string) *ServiceError {
// NewServiceError creates a new ServiceError instance with the given message and status code.
func NewServiceError(message string, statusCode ...int) *ServiceError {
code := http.StatusInternalServerError
if len(statusCode) > 0 {
code = statusCode[0]
}
return &ServiceError{
Message: message,
Message: message,
StatusCode: code,
}
}

0 comments on commit 8a37257

Please sign in to comment.