-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor http client to introduce custom error types (#113)
* start refactor * add deps * Add msw and more tests * Add todo comment * fix deps * Add validation errors * Add sinon pkg * Refactor client sendMessage getOfferings getExchange getExchanges getPfiServiceEndpoint * Add mocks and tests for sendMessage, getOfferings, and getPfiServiceEndpoint * Add tests for getExchange and getExchanges * small revert * small import fix * small revert * remove httpresponse return val from sendmessage * Rename InvalidServiceEndpointError to MissingServiceEndpointError * Remove DataResponse type * Remove msw in favour of sinon stub * Remove msw pkg * Add changeset * Update changeset --------- Co-authored-by: Moe Jangda <moe@tbd.email>
- Loading branch information
1 parent
c3610ed
commit 9e1015e
Showing
12 changed files
with
852 additions
and
497 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"@tbdex/http-client": minor | ||
--- | ||
|
||
Introduces custom errors types and breaking changes: functions now throw instead of return on failure |
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,3 @@ | ||
export { RequestError } from './request-error.js' | ||
export { ResponseError } from './response-error.js' | ||
export { ValidationError, InvalidDidError, MissingServiceEndpointError } from './validation-error.js' |
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,25 @@ | ||
export type RequestErrorParams = { | ||
message: string | ||
recipientDid: string | ||
url?: string | ||
cause?: unknown | ||
} | ||
|
||
/** | ||
* Error thrown when making HTTP requests | ||
* @beta | ||
*/ | ||
export class RequestError extends Error { | ||
public readonly recipientDid: string | ||
public readonly url: string | ||
|
||
constructor(params: RequestErrorParams) { | ||
super(params.message, { cause: params.cause }) | ||
|
||
this.name = this.constructor.name | ||
this.recipientDid = params.recipientDid | ||
this.url = params.url | ||
|
||
Object.setPrototypeOf(this, RequestError.prototype) | ||
} | ||
} |
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,31 @@ | ||
import type { ErrorDetail } from '../types.js' | ||
|
||
export type ResponseErrorParams = { | ||
statusCode: number | ||
details: ErrorDetail[] | ||
recipientDid: string | ||
url: string | ||
} | ||
|
||
/** | ||
* Error thrown when getting HTTP responses | ||
* @beta | ||
*/ | ||
export class ResponseError extends Error { | ||
public readonly statusCode: number | ||
public readonly details: ErrorDetail[] | ||
public readonly recipientDid: string | ||
public readonly url: string | ||
|
||
constructor(params: ResponseErrorParams) { | ||
super() | ||
|
||
this.name = this.constructor.name | ||
this.statusCode = params.statusCode | ||
this.details = params.details | ||
this.recipientDid = params.recipientDid | ||
this.url = params.url | ||
|
||
Object.setPrototypeOf(this, ResponseError.prototype) | ||
} | ||
} |
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,41 @@ | ||
export type ValidationErrorParams = { | ||
message: string | ||
} | ||
|
||
/** | ||
* Error thrown when validating data | ||
* @beta | ||
*/ | ||
export class ValidationError extends Error { | ||
constructor(message: string) { | ||
super(message) | ||
|
||
this.name = this.constructor.name | ||
|
||
Object.setPrototypeOf(this, ValidationError.prototype) | ||
} | ||
} | ||
|
||
/** | ||
* Error thrown when a DID is invalid | ||
* @beta | ||
*/ | ||
export class InvalidDidError extends ValidationError { | ||
constructor(message: string) { | ||
super(message) | ||
|
||
Object.setPrototypeOf(this, InvalidDidError.prototype) | ||
} | ||
} | ||
|
||
/** | ||
* Error thrown when a PFI's service endpoint can't be found | ||
* @beta | ||
*/ | ||
export class MissingServiceEndpointError extends ValidationError { | ||
constructor(message: string) { | ||
super(message) | ||
|
||
Object.setPrototypeOf(this, MissingServiceEndpointError.prototype) | ||
} | ||
} |
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
Oops, something went wrong.