-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7c9a45e
commit 8a37257
Showing
1 changed file
with
19 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
} | ||
} |