-
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.
refactor(web): Improve error handling and shutdown logic in web frame…
…work
- Loading branch information
1 parent
b0a3437
commit 329bbb7
Showing
3 changed files
with
84 additions
and
1 deletion.
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 |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package response | ||
|
||
import "errors" | ||
|
||
type ErrorDocument struct { | ||
Error string `json:"error"` | ||
Fields map[string]string `json:"fields,omitempty"` | ||
} | ||
|
||
type Error struct { | ||
Err error | ||
Status int | ||
} | ||
|
||
func NewError(err error, status int) error { | ||
return &Error{err, status} | ||
} | ||
|
||
func (re *Error) Error() string { | ||
return re.Err.Error() | ||
} | ||
|
||
func IsError(err error) bool { | ||
var re *Error | ||
return errors.As(err, &re) | ||
} | ||
|
||
func GetError(err error) *Error { | ||
var re *Error | ||
if !errors.As(err, &re) { | ||
return nil | ||
} | ||
return re | ||
} |
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,26 @@ | ||
package web | ||
|
||
import "errors" | ||
|
||
// shutdownError is a type used to help with the graceful termination of the service. | ||
type shutdownError struct { | ||
Message string | ||
} | ||
|
||
// NewShutdownError returns an error that causes the framework to signal | ||
// a graceful shutdown. | ||
func NewShutdownError(message string) error { | ||
return &shutdownError{message} | ||
} | ||
|
||
// Error is the implementation of the error interface. | ||
func (se *shutdownError) Error() string { | ||
return se.Message | ||
} | ||
|
||
// IsShutdown checks to see if the shutdown error is contained | ||
// in the specified error value. | ||
func IsShutdown(err error) bool { | ||
var se *shutdownError | ||
return errors.As(err, &se) | ||
} |
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