Skip to content

Commit

Permalink
feat: throw a custom error when parsing the Frontegg response fails (#35
Browse files Browse the repository at this point in the history
)

* throw a custom error when parsing the frontegg response fails

* conditionally include response body

* Include response object keys in error

---------

Co-authored-by: Ondrej Sevcik <[email protected]>
  • Loading branch information
arthuracs and ondrejsevcik authored Sep 19, 2024
1 parent 9943966 commit 030b97a
Showing 1 changed file with 19 additions and 5 deletions.
24 changes: 19 additions & 5 deletions src/frontegg-oauth-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -297,11 +297,25 @@ export class FronteggOAuthClient {
})

const json: unknown = await response.json()
const data = GET_FRONTEGG_TOKEN_RESPONSE_SCHEMA.parse(json)
this.accessToken = data.access_token
this.refreshToken = data.refresh_token
this.tokenExpirationTime = calculateTokenExpirationTime(data.expires_in)
return this.accessToken
try {
const data = GET_FRONTEGG_TOKEN_RESPONSE_SCHEMA.parse(json)

this.accessToken = data.access_token
this.refreshToken = data.refresh_token
this.tokenExpirationTime = calculateTokenExpirationTime(data.expires_in)
return this.accessToken
} catch (error: unknown) {
throw new FronteggError({
text: 'Error while parsing Frontegg response.',
status: 500,
url: `${this.baseUrl}/frontegg/oauth/authorize/silent`,
fronteggTraceId: response.headers.get('frontegg-trace-id') ?? 'undefined',
body: {
responseKeys: typeof json === 'object' && json !== null ? Object.keys(json) : undefined,
error,
},
})
}
}

/**
Expand Down

0 comments on commit 030b97a

Please sign in to comment.