Skip to content

Instantly share code, notes, and snippets.

Created December 26, 2019 22:37
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save L04DB4L4NC3R/c407b1530a0ca915372cd0ba4652dec8 to your computer and use it in GitHub Desktop.
Clean Architecture
package views
import (
log ""
pkg ""
type ErrView struct {
Message string `json:"message"`
Status int `json:"status"`
var (
ErrMethodNotAllowed = errors.New("Error: Method is not allowed")
ErrInvalidToken = errors.New("Error: Invalid Authorization token")
ErrUserExists = errors.New("User already exists")
var ErrHTTPStatusMap = map[string]int{
pkg.ErrNotFound.Error(): http.StatusNotFound,
pkg.ErrInvalidSlug.Error(): http.StatusBadRequest,
pkg.ErrExists.Error(): http.StatusConflict,
pkg.ErrNoContent.Error(): http.StatusNotFound,
pkg.ErrDatabase.Error(): http.StatusInternalServerError,
pkg.ErrUnauthorized.Error(): http.StatusUnauthorized,
pkg.ErrForbidden.Error(): http.StatusForbidden,
ErrMethodNotAllowed.Error(): http.StatusMethodNotAllowed,
ErrInvalidToken.Error(): http.StatusBadRequest,
ErrUserExists.Error(): http.StatusConflict,
func Wrap(err error, w http.ResponseWriter) {
msg := err.Error()
code := ErrHTTPStatusMap[msg]
// If error code is not found
// like a default case
if code == 0 {
code = http.StatusInternalServerError
errView := ErrView{
Message: msg,
Status: code,
"message": msg,
"code": code,
}).Error("Error occurred")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment