forked from DEVRhylme-Foundation/expressify
-
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.
Fixed DEVRhylme-Foundation#23: Improve Error Handling in API Response
- Loading branch information
Showing
6 changed files
with
169 additions
and
56 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
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
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
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
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 |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package errors | ||
|
||
import "fmt" | ||
|
||
// ErrorType represents the type of error that occurred | ||
type ErrorType string | ||
|
||
const ( | ||
ValidationError ErrorType = "VALIDATION_ERROR" | ||
RuntimeError ErrorType = "RUNTIME_ERROR" | ||
SystemError ErrorType = "SYSTEM_ERROR" | ||
) | ||
|
||
// AppError represents a structured error response | ||
type AppError struct { | ||
Type ErrorType `json:"type"` | ||
Message string `json:"message"` | ||
Detail string `json:"detail"` | ||
Code int `json:"code"` | ||
} | ||
|
||
// Error implements the error interface | ||
func (e *AppError) Error() string { | ||
return fmt.Sprintf("[%s] %s: %s", e.Type, e.Message, e.Detail) | ||
} | ||
|
||
// New creates a new AppError | ||
func New(errorType ErrorType, message string, detail string, code int) *AppError { | ||
return &AppError{ | ||
Type: errorType, | ||
Message: message, | ||
Detail: detail, | ||
Code: code, | ||
} | ||
} | ||
|
||
// Common error constructors | ||
func NewValidationError(message string, detail string) *AppError { | ||
return New(ValidationError, message, detail, 400) | ||
} | ||
|
||
func NewRuntimeError(message string, detail string) *AppError { | ||
return New(RuntimeError, message, detail, 500) | ||
} | ||
|
||
func NewSystemError(message string, detail string) *AppError { | ||
return New(SystemError, message, detail, 500) | ||
} |
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