-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
MG-28 - Align Bootstrap with SuperMQ (#31)
* refactor: aligh bootstrap with new supermq architecture Signed-off-by: Felix Gateru <[email protected]> * feat: add sdk and update api docs Signed-off-by: Felix Gateru <[email protected]> * refactor: rename env variables Signed-off-by: Felix Gateru <[email protected]> * style: add empty line to config files and bootstrap docker compose file Signed-off-by: Felix Gateru <[email protected]> * refactor: add supermq sdk to magistrala sdk Signed-off-by: Felix Gateru <[email protected]> * refactor: extend supermq sdk in magistrala sdk Signed-off-by: Felix Gateru <[email protected]> * reafctor: update responses Signed-off-by: Felix Gateru <[email protected]> * ci: update api docs dir in swagger-ui deployment Signed-off-by: Felix Gateru <[email protected]> --------- Signed-off-by: Felix Gateru <[email protected]>
- Loading branch information
1 parent
3f0bb25
commit ec71a5e
Showing
52 changed files
with
7,865 additions
and
298 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
// Copyright (c) Abstract Machines | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package api | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"net/http" | ||
|
||
"github.com/absmach/magistrala/bootstrap" | ||
api "github.com/absmach/supermq/api/http" | ||
apiutil "github.com/absmach/supermq/api/http/util" | ||
"github.com/absmach/supermq/pkg/errors" | ||
) | ||
|
||
// EncodeError encodes an error response. | ||
func EncodeError(ctx context.Context, err error, w http.ResponseWriter) { | ||
var wrapper error | ||
if errors.Contains(err, apiutil.ErrValidation) { | ||
wrapper, err = errors.Unwrap(err) | ||
} | ||
|
||
w.Header().Set("Content-Type", api.ContentType) | ||
|
||
status, nerr := toStatus(err) | ||
if nerr != nil { | ||
err = unwrap(err) | ||
w.WriteHeader(status) | ||
encodeErrorMessage(err, wrapper, w) | ||
return | ||
} | ||
|
||
if wrapper != nil { | ||
err = errors.Wrap(wrapper, err) | ||
} | ||
api.EncodeError(ctx, err, w) | ||
} | ||
|
||
func toStatus(err error) (int, error) { | ||
switch { | ||
case errors.Contains(err, bootstrap.ErrExternalKey), | ||
errors.Contains(err, bootstrap.ErrExternalKeySecure): | ||
return http.StatusForbidden, err | ||
|
||
case errors.Contains(err, bootstrap.ErrBootstrapState), | ||
errors.Contains(err, bootstrap.ErrAddBootstrap): | ||
return http.StatusBadRequest, err | ||
|
||
case errors.Contains(err, bootstrap.ErrBootstrap): | ||
return http.StatusNotFound, err | ||
|
||
default: | ||
return 0, nil | ||
} | ||
} | ||
|
||
func encodeErrorMessage(err, wrapper error, w http.ResponseWriter) { | ||
if wrapper != nil { | ||
err = errors.Wrap(wrapper, err) | ||
} | ||
if errorVal, ok := err.(errors.Error); ok { | ||
if err := json.NewEncoder(w).Encode(errorVal); err != nil { | ||
w.WriteHeader(http.StatusInternalServerError) | ||
} | ||
} | ||
} | ||
|
||
func unwrap(err error) error { | ||
wrapper, err := errors.Unwrap(err) | ||
if wrapper != nil { | ||
return wrapper | ||
} | ||
return err | ||
} |
File renamed without changes.
File renamed without changes.
File renamed without changes.
Oops, something went wrong.