From 5e274dbfd2295e87cc95c9f8f8b61aa5e4689a65 Mon Sep 17 00:00:00 2001 From: Frederik Prijck Date: Tue, 4 Nov 2025 17:05:54 +0100 Subject: [PATCH 1/4] chore: avoid duplication in connected accounts error handling --- src/errors/index.ts | 42 ++++++++++++++++++++++++++++ src/server/auth-client.ts | 59 ++++++--------------------------------- 2 files changed, 51 insertions(+), 50 deletions(-) diff --git a/src/errors/index.ts b/src/errors/index.ts index 45264a1d..8ecafa56 100644 --- a/src/errors/index.ts +++ b/src/errors/index.ts @@ -406,3 +406,45 @@ export class ConnectAccountError extends SdkError { this.cause = cause; } } + +/** + * Builds a ConnectAccountError response based on the provided Response object and error code. + * @param res The Response object containing the error details. + * @param errorCode The ConnectAccountErrorCodes enum value representing the type of error. + * @returns + */ +export async function buildConnectAccountErrorResponse( + res: Response, + errorCode: ConnectAccountErrorCodes +): Promise<[ConnectAccountError, null]> { + const actionVerb = + errorCode === ConnectAccountErrorCodes.FAILED_TO_INITIATE + ? "initiate" + : "complete"; + + try { + const errorBody = await res.json(); + return [ + new ConnectAccountError({ + code: errorCode, + message: `The request to ${actionVerb} the connect account flow failed with status ${res.status}.`, + cause: new MyAccountApiError({ + type: errorBody.type, + title: errorBody.title, + detail: errorBody.detail, + status: res.status, + validationErrors: errorBody.validation_errors + }) + }), + null + ]; + } catch (e) { + return [ + new ConnectAccountError({ + code: errorCode, + message: `The request to ${actionVerb} the connect account flow failed with status ${res.status}.` + }), + null + ]; + } +} diff --git a/src/server/auth-client.ts b/src/server/auth-client.ts index a654cf05..3bf24a6b 100644 --- a/src/server/auth-client.ts +++ b/src/server/auth-client.ts @@ -15,6 +15,7 @@ import { BackchannelAuthenticationError, BackchannelAuthenticationNotSupportedError, BackchannelLogoutError, + buildConnectAccountErrorResponse, ConnectAccountError, ConnectAccountErrorCodes, DiscoveryError, @@ -1946,31 +1947,10 @@ export class AuthClient { }); if (!res.ok) { - try { - const errorBody = await res.json(); - return [ - new ConnectAccountError({ - code: ConnectAccountErrorCodes.FAILED_TO_INITIATE, - message: `The request to initiate the connect account flow failed with status ${res.status}.`, - cause: new MyAccountApiError({ - type: errorBody.type, - title: errorBody.title, - detail: errorBody.detail, - status: res.status, - validationErrors: errorBody.validation_errors - }) - }), - null - ]; - } catch (e) { - return [ - new ConnectAccountError({ - code: ConnectAccountErrorCodes.FAILED_TO_INITIATE, - message: `The request to initiate the connect account flow failed with status ${res.status}.` - }), - null - ]; - } + return buildConnectAccountErrorResponse( + res, + ConnectAccountErrorCodes.FAILED_TO_INITIATE + ); } const { connect_uri, connect_params, auth_session, expires_in } = @@ -2041,31 +2021,10 @@ export class AuthClient { }); if (!res.ok) { - try { - const errorBody = await res.json(); - return [ - new ConnectAccountError({ - code: ConnectAccountErrorCodes.FAILED_TO_COMPLETE, - message: `The request to complete the connect account flow failed with status ${res.status}.`, - cause: new MyAccountApiError({ - type: errorBody.type, - title: errorBody.title, - detail: errorBody.detail, - status: res.status, - validationErrors: errorBody.validation_errors - }) - }), - null - ]; - } catch (e) { - return [ - new ConnectAccountError({ - code: ConnectAccountErrorCodes.FAILED_TO_COMPLETE, - message: `The request to complete the connect account flow failed with status ${res.status}.` - }), - null - ]; - } + return buildConnectAccountErrorResponse( + res, + ConnectAccountErrorCodes.FAILED_TO_COMPLETE + ); } const { id, connection, access_type, scopes, created_at, expires_at } = From 98140126fee5fb14dad9320a874716e88a0e0282 Mon Sep 17 00:00:00 2001 From: Frederik Prijck Date: Tue, 4 Nov 2025 17:25:49 +0100 Subject: [PATCH 2/4] fix linter --- src/server/auth-client.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/src/server/auth-client.ts b/src/server/auth-client.ts index 3bf24a6b..a56ab778 100644 --- a/src/server/auth-client.ts +++ b/src/server/auth-client.ts @@ -23,7 +23,6 @@ import { DPoPErrorCode, InvalidStateError, MissingStateError, - MyAccountApiError, OAuth2Error, SdkError } from "../errors/index.js"; From a22df3b28e9c32b68569c9533b2e1b58f07c4216 Mon Sep 17 00:00:00 2001 From: Frederik Prijck Date: Tue, 4 Nov 2025 17:53:29 +0100 Subject: [PATCH 3/4] remove function from errors.ts as that is exported --- src/errors/index.ts | 42 ------------------------------------- src/server/auth-client.ts | 44 ++++++++++++++++++++++++++++++++++++++- 2 files changed, 43 insertions(+), 43 deletions(-) diff --git a/src/errors/index.ts b/src/errors/index.ts index 8ecafa56..45264a1d 100644 --- a/src/errors/index.ts +++ b/src/errors/index.ts @@ -406,45 +406,3 @@ export class ConnectAccountError extends SdkError { this.cause = cause; } } - -/** - * Builds a ConnectAccountError response based on the provided Response object and error code. - * @param res The Response object containing the error details. - * @param errorCode The ConnectAccountErrorCodes enum value representing the type of error. - * @returns - */ -export async function buildConnectAccountErrorResponse( - res: Response, - errorCode: ConnectAccountErrorCodes -): Promise<[ConnectAccountError, null]> { - const actionVerb = - errorCode === ConnectAccountErrorCodes.FAILED_TO_INITIATE - ? "initiate" - : "complete"; - - try { - const errorBody = await res.json(); - return [ - new ConnectAccountError({ - code: errorCode, - message: `The request to ${actionVerb} the connect account flow failed with status ${res.status}.`, - cause: new MyAccountApiError({ - type: errorBody.type, - title: errorBody.title, - detail: errorBody.detail, - status: res.status, - validationErrors: errorBody.validation_errors - }) - }), - null - ]; - } catch (e) { - return [ - new ConnectAccountError({ - code: errorCode, - message: `The request to ${actionVerb} the connect account flow failed with status ${res.status}.` - }), - null - ]; - } -} diff --git a/src/server/auth-client.ts b/src/server/auth-client.ts index a56ab778..fbd38009 100644 --- a/src/server/auth-client.ts +++ b/src/server/auth-client.ts @@ -15,7 +15,6 @@ import { BackchannelAuthenticationError, BackchannelAuthenticationNotSupportedError, BackchannelLogoutError, - buildConnectAccountErrorResponse, ConnectAccountError, ConnectAccountErrorCodes, DiscoveryError, @@ -23,6 +22,7 @@ import { DPoPErrorCode, InvalidStateError, MissingStateError, + MyAccountApiError, OAuth2Error, SdkError } from "../errors/index.js"; @@ -2229,3 +2229,45 @@ export type FetcherFactoryOptions = { useDPoP?: boolean; getAccessToken: AccessTokenFactory; } & FetcherMinimalConfig; + +/** + * Builds a ConnectAccountError response based on the provided Response object and error code. + * @param res The Response object containing the error details. + * @param errorCode The ConnectAccountErrorCodes enum value representing the type of error. + * @returns + */ +export async function buildConnectAccountErrorResponse( + res: Response, + errorCode: ConnectAccountErrorCodes +): Promise<[ConnectAccountError, null]> { + const actionVerb = + errorCode === ConnectAccountErrorCodes.FAILED_TO_INITIATE + ? "initiate" + : "complete"; + + try { + const errorBody = await res.json(); + return [ + new ConnectAccountError({ + code: errorCode, + message: `The request to ${actionVerb} the connect account flow failed with status ${res.status}.`, + cause: new MyAccountApiError({ + type: errorBody.type, + title: errorBody.title, + detail: errorBody.detail, + status: res.status, + validationErrors: errorBody.validation_errors + }) + }), + null + ]; + } catch (e) { + return [ + new ConnectAccountError({ + code: errorCode, + message: `The request to ${actionVerb} the connect account flow failed with status ${res.status}.` + }), + null + ]; + } +} \ No newline at end of file From 030daa2553af9e6ab18e85a4b76bc92178c245a6 Mon Sep 17 00:00:00 2001 From: Frederik Prijck Date: Tue, 4 Nov 2025 17:54:04 +0100 Subject: [PATCH 4/4] fix linter --- src/server/auth-client.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/server/auth-client.ts b/src/server/auth-client.ts index fbd38009..5bf6cd3c 100644 --- a/src/server/auth-client.ts +++ b/src/server/auth-client.ts @@ -2270,4 +2270,4 @@ export async function buildConnectAccountErrorResponse( null ]; } -} \ No newline at end of file +}