-
Notifications
You must be signed in to change notification settings - Fork 1
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
Showing
14 changed files
with
177 additions
and
111 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 |
---|---|---|
@@ -0,0 +1,99 @@ | ||
package errorhandler | ||
|
||
import ( | ||
"fmt" | ||
"net/http" | ||
) | ||
|
||
const ( | ||
MsgErrMissingParam = "missingParameter" | ||
|
||
MsgErrInvalidQueryParam = "invalidQueryParameter" | ||
MsgErrInvalidPathParam = "invalidPathParameter" | ||
MsgErrInvalidParam = "invalidParameter" | ||
|
||
MsgErrOpNotPermitted = "operationNotPermitted" | ||
AuthHeader = "authorizationHeader" | ||
BasicToken = "basicToken" | ||
BearerToken = "bearerToken" | ||
Token = "token" | ||
Level = "level" | ||
) | ||
|
||
var emitter string | ||
|
||
// SetEmitter is called by the components that call the methods of the common service | ||
func SetEmitter(e string) { | ||
emitter = e | ||
} | ||
|
||
// GetEmitter allows to see who called the common service | ||
func GetEmitter() string { | ||
return emitter | ||
} | ||
|
||
// Error can be returned by the API endpoints | ||
type Error struct { | ||
Status int | ||
Message string | ||
} | ||
|
||
func (e Error) Error() string { | ||
return fmt.Sprintf("%d %s", e.Status, e.Message) | ||
} | ||
|
||
// CreateInternalServerError creates an error relative to an internal server error | ||
func CreateInternalServerError(message string) Error { | ||
return Error{ | ||
Status: http.StatusInternalServerError, | ||
Message: GetEmitter() + "." + message, | ||
} | ||
} | ||
|
||
// CreateMissingParameterError creates an error relative to a missing mandatory parameter | ||
func CreateMissingParameterError(name string) Error { | ||
return Error{ | ||
Status: http.StatusBadRequest, | ||
Message: fmt.Sprintf("%s.%s.%s", GetEmitter(), MsgErrMissingParam, name), | ||
} | ||
} | ||
|
||
// CreateInvalidQueryParameterError creates an error relative to a invalid query parameter | ||
func CreateInvalidQueryParameterError(paramName string) Error { | ||
return Error{ | ||
Status: http.StatusBadRequest, | ||
Message: fmt.Sprintf("%s.%s.%s", GetEmitter(), MsgErrInvalidQueryParam, paramName), | ||
} | ||
} | ||
|
||
// CreateInvalidPathParameterError creates an error relative to a invalid path parameter | ||
func CreateInvalidPathParameterError(paramName string) Error { | ||
return Error{ | ||
Status: http.StatusBadRequest, | ||
Message: fmt.Sprintf("%s.%s.%s", GetEmitter(), MsgErrInvalidPathParam, paramName), | ||
} | ||
} | ||
|
||
// CreateBadRequestError creates an error relative to a bad request | ||
func CreateBadRequestError(publicMessage string) Error { | ||
return Error{ | ||
Status: http.StatusBadRequest, | ||
Message: GetEmitter() + "." + publicMessage, | ||
} | ||
} | ||
|
||
// CreateNotAllowedError creates an error relative to a not allowed request | ||
func CreateNotAllowedError(publicMessage string) Error { | ||
return Error{ | ||
Status: http.StatusMethodNotAllowed, | ||
Message: GetEmitter() + "." + publicMessage, | ||
} | ||
} | ||
|
||
// CreateNotFoundError creates an error relative to a not found request | ||
func CreateNotFoundError(publicMessage string) Error { | ||
return Error{ | ||
Status: http.StatusNotFound, | ||
Message: GetEmitter() + "." + publicMessage, | ||
} | ||
} |
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,37 @@ | ||
package errorhandler | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestHTTPResponse(t *testing.T) { | ||
// Coverage | ||
var error = CreateMissingParameterError("parameter") | ||
assert.Contains(t, error.Error(), error.Message) | ||
|
||
error = CreateBadRequestError("message") | ||
assert.Contains(t, error.Error(), error.Message) | ||
|
||
error = CreateInternalServerError("message") | ||
assert.Contains(t, error.Error(), error.Message) | ||
|
||
error = CreateInvalidQueryParameterError("message") | ||
assert.Contains(t, error.Error(), error.Message) | ||
|
||
error = CreateInvalidPathParameterError("message") | ||
assert.Contains(t, error.Error(), error.Message) | ||
|
||
error = CreateNotAllowedError("message") | ||
assert.Contains(t, error.Error(), error.Message) | ||
|
||
error = CreateNotFoundError("message") | ||
assert.Contains(t, error.Error(), error.Message) | ||
} | ||
|
||
func TestEmitter(t *testing.T) { | ||
var emitter = "component" | ||
SetEmitter(emitter) | ||
assert.Equal(t, GetEmitter(), emitter) | ||
} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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
Oops, something went wrong.