Skip to content

Commit 030b97a

Browse files
Arthur Suermondtondrejsevcik
andauthored
feat: throw a custom error when parsing the Frontegg response fails (#35)
* 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 <ondrej.sevcik@lokalise.com>
1 parent 9943966 commit 030b97a

1 file changed

Lines changed: 19 additions & 5 deletions

File tree

src/frontegg-oauth-client.ts

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -297,11 +297,25 @@ export class FronteggOAuthClient {
297297
})
298298

299299
const json: unknown = await response.json()
300-
const data = GET_FRONTEGG_TOKEN_RESPONSE_SCHEMA.parse(json)
301-
this.accessToken = data.access_token
302-
this.refreshToken = data.refresh_token
303-
this.tokenExpirationTime = calculateTokenExpirationTime(data.expires_in)
304-
return this.accessToken
300+
try {
301+
const data = GET_FRONTEGG_TOKEN_RESPONSE_SCHEMA.parse(json)
302+
303+
this.accessToken = data.access_token
304+
this.refreshToken = data.refresh_token
305+
this.tokenExpirationTime = calculateTokenExpirationTime(data.expires_in)
306+
return this.accessToken
307+
} catch (error: unknown) {
308+
throw new FronteggError({
309+
text: 'Error while parsing Frontegg response.',
310+
status: 500,
311+
url: `${this.baseUrl}/frontegg/oauth/authorize/silent`,
312+
fronteggTraceId: response.headers.get('frontegg-trace-id') ?? 'undefined',
313+
body: {
314+
responseKeys: typeof json === 'object' && json !== null ? Object.keys(json) : undefined,
315+
error,
316+
},
317+
})
318+
}
305319
}
306320

307321
/**

0 commit comments

Comments
 (0)