Skip to content

Commit

Permalink
add errors.go file
Browse files Browse the repository at this point in the history
Signed-off-by: Musilah <[email protected]>
  • Loading branch information
Musilah committed Nov 8, 2023
1 parent a03b656 commit d206fbc
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 74 deletions.
42 changes: 42 additions & 0 deletions ui/api/errors.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package api

import "github.com/mainflux/mainflux/pkg/errors"

var (
errAuthorization = errors.New("missing or invalid credentials provided")
errAuthentication = errors.New("failed to perform authentication over the entity")
errMissingSecret = errors.New("missing secret")
errMissingIdentity = errors.New("missing entity identity")
errLimitSize = errors.New("invalid limit size")
errMissingConfigID = errors.New("missing config id")
errPageSize = errors.New("invalid page size")
errMissingEmail = errors.New("missing email")
errMissingName = errors.New("missing name")
errMissingPassword = errors.New("missing password")
errMissingMetadata = errors.New("missing entity metadata")
errMissingConfirmPassword = errors.New("missing confirm password")
errInvalidResetPassword = errors.New("invalid reset password")
errNameSize = errors.New("invalid name size")
errBearerKey = errors.New("missing or invalid bearer entity key")
errMissingOwner = errors.New("missing owner")
errMissingThingID = errors.New("missing thing id")
errMissingItem = errors.New("missing item")
errMissingChannelID = errors.New("missing channel id")
errMissingUserID = errors.New("missing user id")
errMissingRelation = errors.New("missing relation")
errMissingGroupID = errors.New("missing group id")
errMissingParentID = errors.New("missing parent id")
errMissingDescription = errors.New("missing description")
errMissingThingKey = errors.New("missing thing key")
errMissingExternalID = errors.New("missing external id")
errMissingExternalKey = errors.New("missing external key")
errMissingChannel = errors.New("missing channel")
errMissingPayload = errors.New("missing payload")
errMissingError = errors.New("missing error")
errMissingRefreshToken = errors.New("missing refresh token")
errMissingRef = errors.New("missing ref")
errConflict = errors.New("entity already exists")
errInvalidQueryParams = errors.New("invalid query parameters")
errFileFormat = errors.New("invalid file format")
errInvalidFile = errors.New("unsupported file type")
)
116 changes: 42 additions & 74 deletions ui/api/transport.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,45 +65,9 @@ const (
)

var (
errAuthorization = errors.New("missing or invalid credentials provided")
errAuthentication = errors.New("failed to perform authentication over the entity")
errMissingSecret = errors.New("missing secret")
errMissingIdentity = errors.New("missing entity identity")
errLimitSize = errors.New("invalid limit size")
errMissingConfigID = errors.New("missing config id")
errPageSize = errors.New("invalid page size")
errMissingEmail = errors.New("missing email")
errMissingName = errors.New("missing name")
errMissingPassword = errors.New("missing password")
errMissingMetadata = errors.New("missing entity metadata")
errMissingConfirmPassword = errors.New("missing confirm password")
errInvalidResetPassword = errors.New("invalid reset password")
errNameSize = errors.New("invalid name size")
errBearerKey = errors.New("missing or invalid bearer entity key")
errMissingOwner = errors.New("missing owner")
errMissingThingID = errors.New("missing thing id")
errMissingItem = errors.New("missing item")
errMissingChannelID = errors.New("missing channel id")
errMissingUserID = errors.New("missing user id")
errMissingRelation = errors.New("missing relation")
errMissingGroupID = errors.New("missing group id")
errMissingParentID = errors.New("missing parent id")
errMissingDescription = errors.New("missing description")
errMissingThingKey = errors.New("missing thing key")
errMissingExternalID = errors.New("missing external id")
errMissingExternalKey = errors.New("missing external key")
errMissingChannel = errors.New("missing channel")
errMissingPayload = errors.New("missing payload")
errMissingError = errors.New("missing error")
errMissingRefreshToken = errors.New("missing refresh token")
errMissingRef = errors.New("missing ref")
errConflict = errors.New("entity already exists")
errInvalidQueryParams = errors.New("invalid query parameters")
errFileFormat = errors.New("invalid file format")
errInvalidFile = errors.New("unsupported file type")
clientsHeaderLen = 5
groupsHeaderLen = 3
minRows = 2
clientsHeaderLen = 5
groupsHeaderLen = 3
minRows = 2
)

type number interface {
Expand Down Expand Up @@ -2411,36 +2375,7 @@ func encodeError(_ context.Context, err error, w http.ResponseWriter) {
w.WriteHeader(http.StatusUnauthorized)
case errors.Contains(err, errConflict):
w.WriteHeader(http.StatusConflict)
case errors.Contains(err, errMissingSecret),
errors.Contains(err, errMissingIdentity),
errors.Contains(err, errLimitSize),
errors.Contains(err, errPageSize),
errors.Contains(err, errMissingConfigID),
errors.Contains(err, errMissingMetadata),
errors.Contains(err, errMissingEmail),
errors.Contains(err, errMissingName),
errors.Contains(err, errMissingChannel),
errors.Contains(err, errMissingPayload),
errors.Contains(err, errMissingPassword),
errors.Contains(err, errMissingError),
errors.Contains(err, errMissingRefreshToken),
errors.Contains(err, errMissingRef),
errors.Contains(err, errMissingConfirmPassword),
errors.Contains(err, errNameSize),
errors.Contains(err, errBearerKey),
errors.Contains(err, errMissingOwner),
errors.Contains(err, errMissingItem),
errors.Contains(err, errMissingThingID),
errors.Contains(err, errMissingChannelID),
errors.Contains(err, errMissingUserID),
errors.Contains(err, errMissingRelation),
errors.Contains(err, errMissingGroupID),
errors.Contains(err, errMissingParentID),
errors.Contains(err, errMissingDescription),
errors.Contains(err, errMissingThingKey),
errors.Contains(err, errMissingExternalID),
errors.Contains(err, errMissingExternalKey),
errors.Contains(err, ui.ErrFailedCreate),
case errors.Contains(err, ui.ErrFailedCreate),
errors.Contains(err, ui.ErrFailedRetreive),
errors.Contains(err, ui.ErrFailedUpdate),
errors.Contains(err, ui.ErrFailedEnable),
Expand All @@ -2462,11 +2397,6 @@ func encodeError(_ context.Context, err error, w http.ResponseWriter) {
errors.Contains(err, ui.ErrFailedUnshare):
w.Header().Set("Location", "/error?error="+url.QueryEscape(displayError.Error()))
w.WriteHeader(http.StatusSeeOther)
case errors.Contains(err, errInvalidFile):
w.WriteHeader(http.StatusUnsupportedMediaType)
case errors.Contains(err, errFileFormat):
w.WriteHeader(http.StatusBadRequest)

default:
if e, ok := status.FromError(err); ok {
switch e.Code() {
Expand All @@ -2479,4 +2409,42 @@ func encodeError(_ context.Context, err error, w http.ResponseWriter) {
}
w.WriteHeader(http.StatusInternalServerError)
}

switch err {
case errMissingSecret,
errMissingIdentity,
errLimitSize,
errPageSize,
errMissingConfigID,
errMissingMetadata,
errMissingEmail,
errMissingName,
errMissingChannel,
errMissingPayload,
errMissingPassword,
errMissingError,
errMissingRefreshToken,
errMissingRef,
errMissingConfirmPassword,
errNameSize,
errBearerKey,
errMissingOwner,
errMissingItem,
errMissingThingID,
errMissingChannelID,
errMissingUserID,
errMissingRelation,
errMissingGroupID,
errMissingParentID,
errMissingDescription,
errMissingThingKey,
errMissingExternalID,
errMissingExternalKey:
w.Header().Set("Location", "/error?error="+url.QueryEscape(displayError.Error()))
w.WriteHeader(http.StatusSeeOther)
case errInvalidFile:
w.WriteHeader(http.StatusUnsupportedMediaType)
case errFileFormat:
w.WriteHeader(http.StatusBadRequest)
}
}

0 comments on commit d206fbc

Please sign in to comment.