Skip to content

Commit

Permalink
feat: expose raw response in errors
Browse files Browse the repository at this point in the history
  • Loading branch information
TheUnderScorer committed Jun 17, 2024
1 parent 885b693 commit fd8e352
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 11 deletions.
7 changes: 7 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,10 @@ client
if (isVisitorsError(err)) {
if (err.code === 429) {
// VisitorsError429 type

// You can also access raw response
console.log(err.response);

retryLater(err.retryAfter); // this function needs to be implemented on your side
} else {
console.log('error: ', err.error);
Expand All @@ -128,6 +132,9 @@ client
.then((result) => console.log(result))
.catch((err) => {
if (isEventError(err)) {
// You can also access raw response
console.log(err.response);

console.log(`error ${err.code}: `, err.error.message);
} else {
console.log('unknown error: ', err);
Expand Down
7 changes: 4 additions & 3 deletions src/serverApiClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ export class FingerprintJsServerApiClient {
.then(async (response) => {
const jsonResponse = await response.json()
if (response.status !== 200) {
throw { ...(jsonResponse as EventError), status: response.status } as EventError
throw { ...(jsonResponse as EventError), response, status: response.status } as EventError
}
return jsonResponse as EventResponse
})
Expand All @@ -72,6 +72,7 @@ export class FingerprintJsServerApiClient {
throw err
}
const error = (err as unknown) instanceof Error ? (err as Error).toString() : JSON.stringify(err)

throw {
status: 0,
error: error,
Expand Down Expand Up @@ -110,7 +111,7 @@ export class FingerprintJsServerApiClient {

const jsonResponse = await response.json()

throw { ...(jsonResponse as DeleteVisitorError), status: response.status } as DeleteVisitorError
throw { ...(jsonResponse as DeleteVisitorError), response, status: response.status } as DeleteVisitorError
})
.catch((err) => {
if (isDeleteVisitorError(err)) {
Expand Down Expand Up @@ -153,7 +154,7 @@ export class FingerprintJsServerApiClient {
const retryAfter = response.headers.get('retry-after') || ''
;(jsonResponse as VisitorsError429).retryAfter = retryAfter === '' ? 1 : parseInt(retryAfter)
}
throw { ...(jsonResponse as VisitorsError), status: response.status } as VisitorsError
throw { ...(jsonResponse as VisitorsError), response, status: response.status } as VisitorsError
})
.catch((err: Error) => {
if (isVisitorsError(err)) {
Expand Down
19 changes: 12 additions & 7 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,9 @@ export type DeleteVisitError403 =
status: 403
}

export type VisitorsError = VisitorsError403 | VisitorsError429
export type VisitorsError = WithResponse<VisitorsError403 | VisitorsError429>

export type DeleteVisitorError = DeleteVisitError404 | DeleteVisitError403
export type DeleteVisitorError = WithResponse<DeleteVisitError404 | DeleteVisitError403>

export function isVisitorsError(response: any): response is EventError {
return (
Expand Down Expand Up @@ -94,14 +94,19 @@ export type EventResponse = paths['/events/{request_id}']['get']['responses']['2
export type EventError403 = paths['/events/{request_id}']['get']['responses']['403']['content']['application/json']
export type EventError404 = paths['/events/{request_id}']['get']['responses']['404']['content']['application/json']

type GenericEventError = EventError403 | EventError404
type WithResponse<T> = T & {
response: Response
}

type EventErrorCode<T extends GenericEventError> = T extends EventError403 ? 403 : 404
type GenericEventError = WithResponse<EventError403 | EventError404>

export type EventError<T extends GenericEventError = GenericEventError> = T & {
status: EventErrorCode<T>
}
type EventErrorCode<T extends GenericEventError> = T extends EventError403 ? 403 : 404

export type EventError<T extends GenericEventError = GenericEventError> = WithResponse<
T & {
status: EventErrorCode<T>
}
>
export function isEventError(response: any): response is EventError {
return (
(response?.hasOwnProperty('status') &&
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,7 @@ exports[`[Mocked body] Cast visitor webhook with sample request body 1`] = `
"vpn": {
"methods": {
"auxiliaryMobile": false,
"osMismatch": false,
"publicVPN": false,
"timezoneMismatch": false,
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

exports[`[Mocked response] Get Event Error with bad shape 1`] = `
{
"error": "{"error":"Some text instead og shaped object","status":404}",
"error": "{"error":"Some text instead og shaped object","response":{},"status":404}",
"status": 0,
}
`;
Expand Down Expand Up @@ -34,6 +34,7 @@ exports[`[Mocked response] Get Event with additional signals 1`] = `
"userAgent": "Mozilla/5.0 (Windows NT 6.1; Win64; x64) ....",
},
"confidence": {
"revision": "v1.1",
"score": 0.97,
},
"firstSeenAt": {
Expand Down Expand Up @@ -514,6 +515,7 @@ exports[`[Mocked response] Get Event with request_id 1`] = `
"data": {
"methods": {
"auxiliaryMobile": false,
"osMismatch": false,
"publicVPN": false,
"timezoneMismatch": false,
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ describe('[Mocked response] Delete visitor data', () => {
await expect(client.deleteVisitorData(existingVisitorId)).rejects.toEqual({
status: 0,
error: {
response: expect.any(Response),
error: errorInfo,
status: 404,
},
Expand Down

0 comments on commit fd8e352

Please sign in to comment.