(input: Response | string)
| 377 | * @returns A Promise that resolves to an OAuthError instance |
| 378 | */ |
| 379 | export async function parseErrorResponse(input: Response | string): Promise<OAuthError> { |
| 380 | const statusCode = input instanceof Response ? input.status : undefined; |
| 381 | const body = input instanceof Response ? await input.text() : input; |
| 382 | |
| 383 | try { |
| 384 | const result = OAuthErrorResponseSchema.parse(JSON.parse(body)); |
| 385 | const { error, error_description, error_uri } = result; |
| 386 | const errorClass = OAUTH_ERRORS[error] || ServerError; |
| 387 | return new errorClass(error_description || '', error_uri); |
| 388 | } catch (error) { |
| 389 | // Not a valid OAuth error response, but try to inform the user of the raw data anyway |
| 390 | const errorMessage = `${statusCode ? `HTTP ${statusCode}: ` : ''}Invalid OAuth error response: ${error}. Raw body: ${body}`; |
| 391 | return new ServerError(errorMessage); |
| 392 | } |
| 393 | } |
| 394 | |
| 395 | /** |
| 396 | * Orchestrates the full auth flow with a server. |
no test coverage detected
searching dependent graphs…