Skip to content

Commit f37e01d

Browse files
chore: avoid duplication in connected accounts error handling (#2404)
1 parent 3f07fd5 commit f37e01d

File tree

1 file changed

+50
-50
lines changed

1 file changed

+50
-50
lines changed

src/server/auth-client.ts

Lines changed: 50 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1894,31 +1894,10 @@ export class AuthClient {
18941894
});
18951895

18961896
if (!res.ok) {
1897-
try {
1898-
const errorBody = await res.json();
1899-
return [
1900-
new ConnectAccountError({
1901-
code: ConnectAccountErrorCodes.FAILED_TO_INITIATE,
1902-
message: `The request to initiate the connect account flow failed with status ${res.status}.`,
1903-
cause: new MyAccountApiError({
1904-
type: errorBody.type,
1905-
title: errorBody.title,
1906-
detail: errorBody.detail,
1907-
status: res.status,
1908-
validationErrors: errorBody.validation_errors
1909-
})
1910-
}),
1911-
null
1912-
];
1913-
} catch (e) {
1914-
return [
1915-
new ConnectAccountError({
1916-
code: ConnectAccountErrorCodes.FAILED_TO_INITIATE,
1917-
message: `The request to initiate the connect account flow failed with status ${res.status}.`
1918-
}),
1919-
null
1920-
];
1921-
}
1897+
return buildConnectAccountErrorResponse(
1898+
res,
1899+
ConnectAccountErrorCodes.FAILED_TO_INITIATE
1900+
);
19221901
}
19231902

19241903
const { connect_uri, connect_params, auth_session, expires_in } =
@@ -1989,31 +1968,10 @@ export class AuthClient {
19891968
});
19901969

19911970
if (!res.ok) {
1992-
try {
1993-
const errorBody = await res.json();
1994-
return [
1995-
new ConnectAccountError({
1996-
code: ConnectAccountErrorCodes.FAILED_TO_COMPLETE,
1997-
message: `The request to complete the connect account flow failed with status ${res.status}.`,
1998-
cause: new MyAccountApiError({
1999-
type: errorBody.type,
2000-
title: errorBody.title,
2001-
detail: errorBody.detail,
2002-
status: res.status,
2003-
validationErrors: errorBody.validation_errors
2004-
})
2005-
}),
2006-
null
2007-
];
2008-
} catch (e) {
2009-
return [
2010-
new ConnectAccountError({
2011-
code: ConnectAccountErrorCodes.FAILED_TO_COMPLETE,
2012-
message: `The request to complete the connect account flow failed with status ${res.status}.`
2013-
}),
2014-
null
2015-
];
2016-
}
1971+
return buildConnectAccountErrorResponse(
1972+
res,
1973+
ConnectAccountErrorCodes.FAILED_TO_COMPLETE
1974+
);
20171975
}
20181976

20191977
const { id, connection, access_type, scopes, created_at, expires_at } =
@@ -2549,3 +2507,45 @@ export type FetcherFactoryOptions<TOutput extends Response> = {
25492507
useDPoP?: boolean;
25502508
getAccessToken: AccessTokenFactory;
25512509
} & FetcherMinimalConfig<TOutput>;
2510+
2511+
/**
2512+
* Builds a ConnectAccountError response based on the provided Response object and error code.
2513+
* @param res The Response object containing the error details.
2514+
* @param errorCode The ConnectAccountErrorCodes enum value representing the type of error.
2515+
* @returns
2516+
*/
2517+
export async function buildConnectAccountErrorResponse(
2518+
res: Response,
2519+
errorCode: ConnectAccountErrorCodes
2520+
): Promise<[ConnectAccountError, null]> {
2521+
const actionVerb =
2522+
errorCode === ConnectAccountErrorCodes.FAILED_TO_INITIATE
2523+
? "initiate"
2524+
: "complete";
2525+
2526+
try {
2527+
const errorBody = await res.json();
2528+
return [
2529+
new ConnectAccountError({
2530+
code: errorCode,
2531+
message: `The request to ${actionVerb} the connect account flow failed with status ${res.status}.`,
2532+
cause: new MyAccountApiError({
2533+
type: errorBody.type,
2534+
title: errorBody.title,
2535+
detail: errorBody.detail,
2536+
status: res.status,
2537+
validationErrors: errorBody.validation_errors
2538+
})
2539+
}),
2540+
null
2541+
];
2542+
} catch (e) {
2543+
return [
2544+
new ConnectAccountError({
2545+
code: errorCode,
2546+
message: `The request to ${actionVerb} the connect account flow failed with status ${res.status}.`
2547+
}),
2548+
null
2549+
];
2550+
}
2551+
}

0 commit comments

Comments
 (0)