forked from bluesky-social/atproto
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improve messaging of client metadata loading errors (bluesky-social#3135
) * Improve messaging of client metadata loading errors Fixes bluesky-social#3096 * Support parsing of more fetch() errors
- Loading branch information
1 parent
3303ff1
commit 6226546
Showing
8 changed files
with
187 additions
and
119 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 @@ | ||
--- | ||
"@atproto/oauth-provider": patch | ||
--- | ||
|
||
Improve "invalid_client_metadata" error description |
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 @@ | ||
--- | ||
"@atproto-labs/fetch": patch | ||
--- | ||
|
||
Support parsing of more fetch() errors |
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 |
---|---|---|
@@ -1,59 +1,13 @@ | ||
export class FetchError extends Error { | ||
public readonly statusCode: number | ||
|
||
constructor(statusCode?: number, message?: string, options?: ErrorOptions) { | ||
if (statusCode == null || !message) { | ||
const info = extractInfo(extractRootCause(options?.cause)) | ||
statusCode = statusCode ?? info[0] | ||
message = message || info[1] | ||
} | ||
|
||
super(message, options) | ||
|
||
this.statusCode = statusCode | ||
} | ||
} | ||
|
||
function extractRootCause(err: unknown): unknown { | ||
// Unwrap the Network error from undici (i.e. Node's internal fetch() implementation) | ||
// https://github.com/nodejs/undici/blob/3274c975947ce11a08508743df026f73598bfead/lib/web/fetch/index.js#L223-L228 | ||
if ( | ||
err instanceof TypeError && | ||
err.message === 'fetch failed' && | ||
err.cause !== undefined | ||
export abstract class FetchError extends Error { | ||
constructor( | ||
public readonly statusCode: number, | ||
message?: string, | ||
options?: ErrorOptions, | ||
) { | ||
return err.cause | ||
} | ||
|
||
return err | ||
} | ||
|
||
function extractInfo(err: unknown): [statusCode: number, message: string] { | ||
if (typeof err === 'string' && err.length > 0) { | ||
return [500, err] | ||
} | ||
|
||
if (!(err instanceof Error)) { | ||
return [500, 'Failed to fetch'] | ||
super(message, options) | ||
} | ||
|
||
const code = err['code'] | ||
if (typeof code === 'string') { | ||
switch (true) { | ||
case code === 'ENOTFOUND': | ||
return [400, 'Invalid hostname'] | ||
case code === 'ECONNREFUSED': | ||
return [502, 'Connection refused'] | ||
case code === 'DEPTH_ZERO_SELF_SIGNED_CERT': | ||
return [502, 'Self-signed certificate'] | ||
case code.startsWith('ERR_TLS'): | ||
return [502, 'TLS error'] | ||
case code.startsWith('ECONN'): | ||
return [502, 'Connection error'] | ||
default: | ||
return [500, `${code} error`] | ||
} | ||
get expose() { | ||
return true | ||
} | ||
|
||
return [500, err.message] | ||
} |
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
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,7 @@ | ||
export async function callAsync<F extends (...args: any[]) => unknown>( | ||
this: ThisParameterType<F>, | ||
fn: F, | ||
...args: Parameters<F> | ||
): Promise<Awaited<ReturnType<F>>> { | ||
return await (fn(...args) as ReturnType<F>) | ||
} |
Oops, something went wrong.