Skip to content

Commit

Permalink
chore: Implements HTTP error iota const (#27)
Browse files Browse the repository at this point in the history
  • Loading branch information
deepakvashist committed Mar 23, 2021
1 parent 660573e commit d57bde7
Showing 1 changed file with 23 additions and 11 deletions.
34 changes: 23 additions & 11 deletions http/handler/error.go
Expand Up @@ -2,47 +2,59 @@ package handler

import "net/http"

type errorResponseCode int

const (
internalServerErrorCode errorResponseCode = iota + 1
invalidIDCode
malformedRequestBodyCode
requestValidationFailedCode
webhookNotFoundCode
deliveryNotFoundCode
deliveryAttemptNotFoundCode
)

var errorResponses = map[string]errorResponse{
"internal_server_error": {
Code: 1,
Code: internalServerErrorCode,
Message: "internal server error",
StatusCode: http.StatusInternalServerError,
},
"invalid_id": {
Code: 2,
Code: invalidIDCode,
Message: "invalid id",
StatusCode: http.StatusNotFound,
},
"malformed_request_body": {
Code: 3,
Code: malformedRequestBodyCode,
Message: "malformed request body",
StatusCode: http.StatusBadRequest,
},
"request_validation_failed": {
Code: 4,
Code: requestValidationFailedCode,
Message: "request validation failed",
StatusCode: http.StatusBadRequest,
},
"webhook_not_found": {
Code: 5,
Code: webhookNotFoundCode,
Message: "webhook not found",
StatusCode: http.StatusNotFound,
},
"delivery_not_found": {
Code: 6,
Code: deliveryNotFoundCode,
Message: "delivery not found",
StatusCode: http.StatusNotFound,
},
"delivery_attempt_not_found": {
Code: 7,
Code: deliveryAttemptNotFoundCode,
Message: "delivery attempt not found",
StatusCode: http.StatusNotFound,
},
}

type errorResponse struct {
Code int `json:"code"`
Message string `json:"message"`
Details string `json:"details,omitempty"`
StatusCode int `json:"-"`
Code errorResponseCode `json:"code"`
Message string `json:"message"`
Details string `json:"details,omitempty"`
StatusCode int `json:"-"`
} //@name Error

0 comments on commit d57bde7

Please sign in to comment.