Skip to content

Commit

Permalink
Refactor: Error types
Browse files Browse the repository at this point in the history
Signed-off-by: nyagamunene <[email protected]>
  • Loading branch information
nyagamunene committed Jun 12, 2024
1 parent 1a5997a commit ac0a377
Show file tree
Hide file tree
Showing 8 changed files with 1,244 additions and 326 deletions.
239 changes: 209 additions & 30 deletions bootstrap/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,36 +17,215 @@ import (
mgsdk "github.com/absmach/magistrala/pkg/sdk/go"
)

var (
// ErrThings indicates failure to communicate with Magistrala Things service.
// It can be due to networking error or invalid/unauthenticated request.
ErrThings = errors.New("failed to receive response from Things service")

// ErrExternalKey indicates a non-existent bootstrap configuration for given external key.
ErrExternalKey = errors.New("failed to get bootstrap configuration for given external key")

// ErrExternalKeySecure indicates error in getting bootstrap configuration for given encrypted external key.
ErrExternalKeySecure = errors.New("failed to get bootstrap configuration for given encrypted external key")

// ErrBootstrap indicates error in getting bootstrap configuration.
ErrBootstrap = errors.New("failed to read bootstrap configuration")

// ErrAddBootstrap indicates error in adding bootstrap configuration.
ErrAddBootstrap = errors.New("failed to add bootstrap configuration")

errUpdateConnections = errors.New("failed to update connections")
errRemoveBootstrap = errors.New("failed to remove bootstrap configuration")
errChangeState = errors.New("failed to change state of bootstrap configuration")
errUpdateChannel = errors.New("failed to update channel")
errRemoveConfig = errors.New("failed to remove bootstrap configuration")
errRemoveChannel = errors.New("failed to remove channel")
errCreateThing = errors.New("failed to create thing")
errDisconnectThing = errors.New("failed to disconnect thing")
errCheckChannels = errors.New("failed to check if channels exists")
errConnectionChannels = errors.New("failed to check channels connections")
errThingNotFound = errors.New("failed to find thing")
errUpdateCert = errors.New("failed to update cert")
)
// ErrThings indicates failure to communicate with Magistrala Things service.
// It can be due to networking error or invalid/unauthenticated request.
type ThingsError struct {
Err errors.Error
}

func (te *ThingsError) Error() string {
return te.Err.Error()
}

var ErrThings = &ThingsError{
Err: errors.New("failed to receive response from Things service"),
}

// ErrExternalKey indicates a non-existent bootstrap configuration for given external key.
type ExternalKeyError struct {
Err errors.Error
}

func (eke *ExternalKeyError) Error() string {
return eke.Err.Error()
}

var ErrExternalKey = &ExternalKeyError{
Err: errors.New("failed to get bootstrap configuration for given external key"),
}

// ErrExternalKeySecure indicates error in getting bootstrap configuration for given encrypted external key.
type ExternalKeySecureError struct {
Err errors.Error
}

func (ekse *ExternalKeySecureError) Error() string {
return ekse.Err.Error()
}

var ErrExternalKeySecure = &ExternalKeySecureError{
Err: errors.New("failed to get bootstrap configuration for given encrypted external key"),
}

// ErrBootstrap indicates error in getting bootstrap configuration.
type BootstrapError struct {
Err errors.Error
}

func (be *BootstrapError) Error() string {
return be.Err.Error()
}

var ErrBootstrap = &BootstrapError{
Err: errors.New("failed to read bootstrap configuration"),
}

// ErrAddBootstrap indicates error in adding bootstrap configuration.
type AddBootstrapError struct {
Err errors.Error
}

func (abe *AddBootstrapError) Error() string {
return abe.Err.Error()
}

var ErrAddBootstrap = &AddBootstrapError{
Err: errors.New("failed to add bootstrap configuration"),
}

type UpdateConnectionsError struct {
Err errors.Error
}

func (uce *UpdateConnectionsError) Error() string {
return uce.Err.Error()
}

var errUpdateConnections = &UpdateConnectionsError{
Err: errors.New("failed to update connections"),
}

type RemoveBootstrapError struct {
Err errors.Error
}

func (rbe *RemoveBootstrapError) Error() string {
return rbe.Err.Error()
}

var errRemoveBootstrap = &RemoveBootstrapError{
Err: errors.New("failed to remove bootstrap configuration"),
}

type ChangeStateError struct {
Err errors.Error
}

func (cse *ChangeStateError) Error() string {
return cse.Err.Error()
}

var errChangeState = &ChangeStateError{
Err: errors.New("failed to change state of bootstrap configuration"),
}

type UpdateChannelError struct {
Err errors.Error
}

func (uce *UpdateChannelError) Error() string {
return uce.Err.Error()
}

var errUpdateChannel = &UpdateChannelError{
Err: errors.New("failed to update channel"),
}

type RemoveConfigError struct {
Err errors.Error
}

func (rce *RemoveConfigError) Error() string {
return rce.Err.Error()
}

var errRemoveConfig = &RemoveConfigError{
Err: errors.New("failed to remove bootstrap configuration"),
}

type RemoveChannelError struct {
Err errors.Error
}

func (rce *RemoveChannelError) Error() string {
return rce.Err.Error()
}

var errRemoveChannel = &RemoveChannelError{
Err: errors.New("failed to remove channel"),
}

type CreateThingError struct {
Err errors.Error
}

func (cte *CreateThingError) Error() string {
return cte.Err.Error()
}

var errCreateThing = &CreateThingError{
Err: errors.New("failed to create thing"),
}

type DisconnectThingError struct {
Err errors.Error
}

func (dte *DisconnectThingError) Error() string {
return dte.Err.Error()
}

var errDisconnectThing = &DisconnectThingError{
Err: errors.New("failed to disconnect thing"),
}

type CheckChannelsError struct {
Err errors.Error
}

func (cce *CheckChannelsError) Error() string {
return cce.Err.Error()
}

var errCheckChannels = &CheckChannelsError{
Err: errors.New("failed to check if channels exists"),
}

type ConnectionChannelsError struct {
Err errors.Error
}

func (cce *ConnectionChannelsError) Error() string {
return cce.Err.Error()
}

var errConnectionChannels = &ConnectionChannelsError{
Err: errors.New("failed to check channels connections"),
}

type ThingNotFoundError struct {
Err errors.Error
}

func (tnfe *ThingNotFoundError) Error() string {
return tnfe.Err.Error()
}

var errThingNotFound = &ThingNotFoundError{
Err: errors.New("failed to find thing"),
}

type UpdateCertError struct {
Err errors.Error
}

func (uce *UpdateCertError) Error() string {
return uce.Err.Error()
}

var errUpdateCert = &UpdateCertError{
Err: errors.New("failed to update cert"),
}

var _ Service = (*bootstrapService)(nil)

Expand Down
101 changes: 48 additions & 53 deletions internal/api/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,76 +105,71 @@ func EncodeError(_ context.Context, err error, w http.ResponseWriter) {
}

w.Header().Set("Content-Type", ContentType)
switch {
case errors.Contains(err, svcerr.ErrAuthorization),
errors.Contains(err, svcerr.ErrDomainAuthorization),
errors.Contains(err, bootstrap.ErrExternalKey),
errors.Contains(err, bootstrap.ErrExternalKeySecure):
switch err.(type) {
case *svcerr.AuthorizationError, *svcerr.DomainAuthorizationError, *bootstrap.ExternalKeyError, *bootstrap.ExternalKeySecureError:
err = unwrap(err)
w.WriteHeader(http.StatusForbidden)

case errors.Contains(err, svcerr.ErrAuthentication),
errors.Contains(err, apiutil.ErrBearerToken),
errors.Contains(err, svcerr.ErrLogin):
case *svcerr.AuthenticationError, *apiutil.BearerTokenError, *svcerr.LoginError:
err = unwrap(err)
w.WriteHeader(http.StatusUnauthorized)
case errors.Contains(err, svcerr.ErrMalformedEntity),
errors.Contains(err, apiutil.ErrMalformedPolicy),
errors.Contains(err, apiutil.ErrMissingSecret),
errors.Contains(err, errors.ErrMalformedEntity),
errors.Contains(err, apiutil.ErrMissingID),
errors.Contains(err, apiutil.ErrMissingName),
errors.Contains(err, apiutil.ErrMissingEmail),
errors.Contains(err, apiutil.ErrMissingHost),
errors.Contains(err, apiutil.ErrInvalidResetPass),
errors.Contains(err, apiutil.ErrEmptyList),
errors.Contains(err, apiutil.ErrMissingMemberKind),
errors.Contains(err, apiutil.ErrMissingMemberType),
errors.Contains(err, apiutil.ErrLimitSize),
errors.Contains(err, apiutil.ErrBearerKey),
errors.Contains(err, svcerr.ErrInvalidStatus),
errors.Contains(err, apiutil.ErrNameSize),
errors.Contains(err, apiutil.ErrInvalidIDFormat),
errors.Contains(err, apiutil.ErrInvalidQueryParams),
errors.Contains(err, apiutil.ErrMissingRelation),
errors.Contains(err, apiutil.ErrValidation),
errors.Contains(err, apiutil.ErrMissingIdentity),
errors.Contains(err, apiutil.ErrMissingPass),
errors.Contains(err, apiutil.ErrMissingConfPass),
errors.Contains(err, apiutil.ErrPasswordFormat),
errors.Contains(err, svcerr.ErrInvalidRole),
errors.Contains(err, svcerr.ErrInvalidPolicy),
errors.Contains(err, apiutil.ErrInvitationState),
errors.Contains(err, apiutil.ErrInvalidAPIKey),
errors.Contains(err, svcerr.ErrViewEntity),
errors.Contains(err, apiutil.ErrBootstrapState),
errors.Contains(err, apiutil.ErrMissingCertData),
errors.Contains(err, apiutil.ErrInvalidContact),
errors.Contains(err, apiutil.ErrInvalidTopic),
errors.Contains(err, bootstrap.ErrAddBootstrap),
errors.Contains(err, apiutil.ErrInvalidCertData),
errors.Contains(err, apiutil.ErrEmptyMessage):
case *svcerr.MalformedEntityError,
*apiutil.MalformedPolicyError,
*apiutil.MissingSecretError,
*errors.MalformedEntityError,
*apiutil.MissingIDError,
*apiutil.MissingNameError,
*apiutil.MissingEmailError,
*apiutil.MissingHostError,
*apiutil.InvalidResetPassError,
*apiutil.EmptyListError,
*apiutil.MissingMemberKindError,
*apiutil.MissingMemberTypeError,
*apiutil.LimitSizeError,
*apiutil.BearerKeyError,
*svcerr.InvalidStatusError,
*apiutil.NameSizeError,
*apiutil.InvalidIDFormatError,
*apiutil.InvalidQueryParamsError,
*apiutil.MissingRelationError,
*apiutil.ValidationError,
*apiutil.MissingIdentityError,
*apiutil.MissingPassError,
*apiutil.MissingConfPassError,
*apiutil.PasswordFormatError,
*svcerr.InvalidRoleError,
*svcerr.InvalidPolicyError,
*apiutil.InvitationStateError,
*apiutil.InvalidAPIKeyError,
*svcerr.ViewEntityError,
*apiutil.BootstrapStateError,
*apiutil.MissingCertDataError,
*apiutil.InvalidContactError,
*apiutil.InvalidTopicError,
*bootstrap.AddBootstrapError,
*apiutil.InvalidCertDataError,
*apiutil.EmptyMessageError:
err = unwrap(err)
w.WriteHeader(http.StatusBadRequest)

case errors.Contains(err, svcerr.ErrCreateEntity),
errors.Contains(err, svcerr.ErrUpdateEntity),
errors.Contains(err, svcerr.ErrRemoveEntity),
errors.Contains(err, svcerr.ErrEnableClient):
case *svcerr.CreateEntityError,
*svcerr.UpdateEntityError,
*svcerr.RemoveEntityError,
*svcerr.EnableClientError:
err = unwrap(err)
w.WriteHeader(http.StatusUnprocessableEntity)

case errors.Contains(err, svcerr.ErrNotFound),
errors.Contains(err, bootstrap.ErrBootstrap):
case *svcerr.NotFoundError,
*bootstrap.BootstrapError:
err = unwrap(err)
w.WriteHeader(http.StatusNotFound)

case errors.Contains(err, errors.ErrStatusAlreadyAssigned),
errors.Contains(err, svcerr.ErrConflict):
case *errors.StatusAlreadyAssignedError,
*svcerr.ConflictError:
err = unwrap(err)
w.WriteHeader(http.StatusConflict)

case errors.Contains(err, apiutil.ErrUnsupportedContentType):
case *apiutil.UnsupportedContentTypeError:
err = unwrap(err)
w.WriteHeader(http.StatusUnsupportedMediaType)

Expand Down
Loading

0 comments on commit ac0a377

Please sign in to comment.