Skip to content

Commit

Permalink
fix: adds error mapping case for non-json content-type responses
Browse files Browse the repository at this point in the history
  • Loading branch information
ctran88 committed Nov 26, 2024
1 parent 053d095 commit 0fb435d
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 2 deletions.
14 changes: 12 additions & 2 deletions auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package passage

import (
"context"
"encoding/json"
"errors"
"fmt"

Expand All @@ -11,8 +12,8 @@ import (

type auth struct {
appID string
jwksCacheSet jwk.Set
client *ClientWithResponses
jwksCacheSet jwk.Set
}

func newAuth(appID string, client *ClientWithResponses) (*auth, error) {
Expand All @@ -29,8 +30,9 @@ func newAuth(appID string, client *ClientWithResponses) (*auth, error) {
}

auth := auth{
jwksCacheSet: jwk.NewCachedSet(cache, url),
appID: appID,
client: client,
jwksCacheSet: jwk.NewCachedSet(cache, url),
}

return &auth, nil
Expand Down Expand Up @@ -62,6 +64,14 @@ func (a *auth) CreateMagicLink(createMagicLinkBody CreateMagicLinkBody) (*MagicL
case res.JSON500 != nil:
message = res.JSON500.Error
errorCode = string(res.JSON500.Code)
default:
var errorBody httpErrorBody
if err := json.Unmarshal(res.Body, &errorBody); err != nil {
return nil, err
}

message = errorBody.Error
errorCode = errorBody.Code
}

return nil, PassageError{
Expand Down
5 changes: 5 additions & 0 deletions passage_error.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ import (
"strings"
)

type httpErrorBody struct {
Code string `json:"code"`
Error string `json:"error"`
}

type PassageError struct {
Message string
ErrorCode string
Expand Down
81 changes: 81 additions & 0 deletions user.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package passage

import (
"context"
"encoding/json"
"net/http"
"strings"
)
Expand Down Expand Up @@ -45,6 +46,14 @@ func (u *user) Get(userID string) (*PassageUser, error) {
case res.JSON500 != nil:
message = res.JSON500.Error
errorCode = string(res.JSON500.Code)
default:
var errorBody httpErrorBody
if err := json.Unmarshal(res.Body, &errorBody); err != nil {
return nil, err
}

message = errorBody.Error
errorCode = errorBody.Code
}

return nil, PassageError{
Expand Down Expand Up @@ -97,6 +106,14 @@ func (u *user) GetByIdentifier(identifier string) (*PassageUser, error) {
case res.JSON500 != nil:
message = res.JSON500.Error
errorCode = string(res.JSON500.Code)
default:
var errorBody httpErrorBody
if err := json.Unmarshal(res.Body, &errorBody); err != nil {
return nil, err
}

message = errorBody.Error
errorCode = errorBody.Code
}

return nil, PassageError{
Expand Down Expand Up @@ -129,6 +146,14 @@ func (u *user) Activate(userID string) (*PassageUser, error) {
case res.JSON500 != nil:
message = res.JSON500.Error
errorCode = string(res.JSON500.Code)
default:
var errorBody httpErrorBody
if err := json.Unmarshal(res.Body, &errorBody); err != nil {
return nil, err
}

message = errorBody.Error
errorCode = errorBody.Code
}

return nil, PassageError{
Expand Down Expand Up @@ -161,6 +186,14 @@ func (u *user) Deactivate(userID string) (*PassageUser, error) {
case res.JSON500 != nil:
message = res.JSON500.Error
errorCode = string(res.JSON500.Code)
default:
var errorBody httpErrorBody
if err := json.Unmarshal(res.Body, &errorBody); err != nil {
return nil, err
}

message = errorBody.Error
errorCode = errorBody.Code
}

return nil, PassageError{
Expand Down Expand Up @@ -196,6 +229,14 @@ func (u *user) Update(userID string, options UpdateUserOptions) (*PassageUser, e
case res.JSON500 != nil:
message = res.JSON500.Error
errorCode = string(res.JSON500.Code)
default:
var errorBody httpErrorBody
if err := json.Unmarshal(res.Body, &errorBody); err != nil {
return nil, err
}

message = errorBody.Error
errorCode = errorBody.Code
}

return nil, PassageError{
Expand Down Expand Up @@ -231,6 +272,14 @@ func (u *user) Create(args CreateUserArgs) (*PassageUser, error) {
case res.JSON500 != nil:
message = res.JSON500.Error
errorCode = string(res.JSON500.Code)
default:
var errorBody httpErrorBody
if err := json.Unmarshal(res.Body, &errorBody); err != nil {
return nil, err
}

message = errorBody.Error
errorCode = errorBody.Code
}

return nil, PassageError{
Expand Down Expand Up @@ -263,6 +312,14 @@ func (u *user) Delete(userID string) error {
case res.JSON500 != nil:
message = res.JSON500.Error
errorCode = string(res.JSON500.Code)
default:
var errorBody httpErrorBody
if err := json.Unmarshal(res.Body, &errorBody); err != nil {
return err
}

message = errorBody.Error
errorCode = errorBody.Code
}

return PassageError{
Expand Down Expand Up @@ -295,6 +352,14 @@ func (u *user) ListDevices(userID string) ([]WebAuthnDevices, error) {
case res.JSON500 != nil:
message = res.JSON500.Error
errorCode = string(res.JSON500.Code)
default:
var errorBody httpErrorBody
if err := json.Unmarshal(res.Body, &errorBody); err != nil {
return nil, err
}

message = errorBody.Error
errorCode = errorBody.Code
}

return nil, PassageError{
Expand Down Expand Up @@ -327,6 +392,14 @@ func (u *user) RevokeDevice(userID string, deviceID string) error {
case res.JSON500 != nil:
message = res.JSON500.Error
errorCode = string(res.JSON500.Code)
default:
var errorBody httpErrorBody
if err := json.Unmarshal(res.Body, &errorBody); err != nil {
return err
}

message = errorBody.Error
errorCode = errorBody.Code
}

return PassageError{
Expand Down Expand Up @@ -359,6 +432,14 @@ func (u *user) RevokeRefreshTokens(userID string) error {
case res.JSON500 != nil:
message = res.JSON500.Error
errorCode = string(res.JSON500.Code)
default:
var errorBody httpErrorBody
if err := json.Unmarshal(res.Body, &errorBody); err != nil {
return err
}

message = errorBody.Error
errorCode = errorBody.Code
}

return PassageError{
Expand Down

0 comments on commit 0fb435d

Please sign in to comment.