This repository has been archived by the owner on May 2, 2023. It is now read-only.
-
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
mohammadne
committed
Sep 12, 2021
1 parent
aade55c
commit 34acdb6
Showing
2 changed files
with
126 additions
and
15 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,95 @@ | ||
package jwt | ||
|
||
import ( | ||
"fmt" | ||
"net/http" | ||
|
||
"github.com/mohammadne/bookman/auth/pkg/failures" | ||
) | ||
|
||
type failure struct { | ||
FailureMessage string `json:"message"` | ||
FailureStatus int `json:"status"` | ||
FailureCauses []string `json:"causes"` | ||
} | ||
|
||
// ==============================================================> methods | ||
|
||
func (f *failure) Message() string { | ||
return f.FailureMessage | ||
} | ||
|
||
func (f *failure) Status() int { | ||
return f.FailureStatus | ||
} | ||
|
||
func (f *failure) Causes() []string { | ||
return f.FailureCauses | ||
} | ||
|
||
func (f *failure) Error() string { | ||
return fmt.Sprintf( | ||
"message: %s - status: %d - causes: %v", | ||
f.FailureMessage, f.FailureStatus, f.FailureCauses, | ||
) | ||
} | ||
|
||
// ==============================================================> constructors | ||
|
||
type Failure struct{} | ||
|
||
func (Failure) New(message string, status int, causes []string) failures.Failure { | ||
return &failure{ | ||
FailureMessage: message, | ||
FailureStatus: status, | ||
FailureCauses: causes, | ||
} | ||
} | ||
|
||
func (Failure) NewBadRequest(message string) failures.Failure { | ||
return &failure{ | ||
FailureMessage: message, | ||
FailureStatus: http.StatusBadRequest, | ||
} | ||
} | ||
|
||
func (Failure) NewNotFound(message string) failures.Failure { | ||
return &failure{ | ||
FailureMessage: message, | ||
FailureStatus: http.StatusNotFound, | ||
} | ||
} | ||
|
||
func (Failure) NewUnauthorized(message string) failures.Failure { | ||
return &failure{ | ||
FailureMessage: message, | ||
FailureStatus: http.StatusUnauthorized, | ||
} | ||
} | ||
|
||
func (Failure) NewUnprocessableEntity(message string) failures.Failure { | ||
return &failure{ | ||
FailureMessage: message, | ||
FailureStatus: http.StatusUnprocessableEntity, | ||
} | ||
} | ||
|
||
func (Failure) NewNotImplemented() failures.Failure { | ||
return &failure{ | ||
FailureMessage: "not implemented", | ||
FailureStatus: http.StatusNotImplemented, | ||
} | ||
} | ||
|
||
func (Failure) NewInternalServer(message string, errors ...error) failures.Failure { | ||
causes := make([]string, 0, len(errors)) | ||
for index := 0; index < len(errors); index++ { | ||
causes = append(causes, errors[index].Error()) | ||
} | ||
|
||
return &failure{ | ||
FailureMessage: message, | ||
FailureStatus: http.StatusInternalServerError, | ||
FailureCauses: causes, | ||
} | ||
} |
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