-
Notifications
You must be signed in to change notification settings - Fork 13
[ISSUE-169] fix: us link account form #487
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -65,31 +65,40 @@ export async function POST(request: NextRequest) { | |||||||||||||||||||||||||||||||
body: JSON.stringify(body), | ||||||||||||||||||||||||||||||||
}) | ||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||
const data = await response.json() | ||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||
if (!response.ok) { | ||||||||||||||||||||||||||||||||
if (data.code && data.code == 'duplicate_external_account') { | ||||||||||||||||||||||||||||||||
// in case the bridge account already exists | ||||||||||||||||||||||||||||||||
// | ||||||||||||||||||||||||||||||||
// sending back error responses is not currently common across app | ||||||||||||||||||||||||||||||||
// in how we deliver errors from backend -> frontend | ||||||||||||||||||||||||||||||||
// sends back an object like: | ||||||||||||||||||||||||||||||||
// { | ||||||||||||||||||||||||||||||||
// id: '4c537540-80bf-41dd-a528-dbe39a4', | ||||||||||||||||||||||||||||||||
// code: 'duplicate_external_account', | ||||||||||||||||||||||||||||||||
// message: 'An external account with the same information has already been added for this customer' | ||||||||||||||||||||||||||||||||
// } | ||||||||||||||||||||||||||||||||
// | ||||||||||||||||||||||||||||||||
// TODO: standardize error responses across backend | ||||||||||||||||||||||||||||||||
return new NextResponse(JSON.stringify(data), { | ||||||||||||||||||||||||||||||||
status: response.status, | ||||||||||||||||||||||||||||||||
headers: { | ||||||||||||||||||||||||||||||||
'Content-Type': 'application/json', | ||||||||||||||||||||||||||||||||
}, | ||||||||||||||||||||||||||||||||
}) | ||||||||||||||||||||||||||||||||
try { | ||||||||||||||||||||||||||||||||
const data = await response.json() | ||||||||||||||||||||||||||||||||
if (data.code && data.code == 'duplicate_external_account') { | ||||||||||||||||||||||||||||||||
// in case the bridge account already exists | ||||||||||||||||||||||||||||||||
// | ||||||||||||||||||||||||||||||||
// sending back error responses is not currently common across app | ||||||||||||||||||||||||||||||||
// in how we deliver errors from backend -> frontend | ||||||||||||||||||||||||||||||||
// sends back an object like: | ||||||||||||||||||||||||||||||||
// { | ||||||||||||||||||||||||||||||||
// id: '4c537540-80bf-41dd-a528-dbe39a4', | ||||||||||||||||||||||||||||||||
// code: 'duplicate_external_account', | ||||||||||||||||||||||||||||||||
// message: 'An external account with the same information has already been added for this customer' | ||||||||||||||||||||||||||||||||
// } | ||||||||||||||||||||||||||||||||
// | ||||||||||||||||||||||||||||||||
// TODO: standardize error responses across backend | ||||||||||||||||||||||||||||||||
return new NextResponse(JSON.stringify(data), { | ||||||||||||||||||||||||||||||||
status: response.status, | ||||||||||||||||||||||||||||||||
headers: { | ||||||||||||||||||||||||||||||||
'Content-Type': 'application/json', | ||||||||||||||||||||||||||||||||
}, | ||||||||||||||||||||||||||||||||
}) | ||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||
} catch (e) { | ||||||||||||||||||||||||||||||||
console.error('Error creating external account', e) | ||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||
throw new Error(`HTTP error! status: ${response.status}`) | ||||||||||||||||||||||||||||||||
return new NextResponse('', { | ||||||||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. question (non-blocking): is an empty data field here enough to handle all errors to function it returns it too? wdyt? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. as an additional note that's visible in the TODOs in these diffs: we need a std way to handle these errors and propagate that error state in the whole UI. I'm currently creating a prototype of what a solution could look like in tl;dr: don't waste too much time on this There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. the empty data field is not enough and I love that you are doing something about the error state, it is needed There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. cool! |
||||||||||||||||||||||||||||||||
status: response.status, | ||||||||||||||||||||||||||||||||
headers: { | ||||||||||||||||||||||||||||||||
'Content-Type': 'application/json', | ||||||||||||||||||||||||||||||||
}, | ||||||||||||||||||||||||||||||||
}) | ||||||||||||||||||||||||||||||||
Comment on lines
+94
to
+99
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Consider preserving error details in non-duplicate error cases. The current implementation returns an empty response for non-duplicate errors, which might make debugging harder for clients. -return new NextResponse('', {
+return new NextResponse(JSON.stringify({
+ code: 'external_account_error',
+ message: `Failed to create external account (status: ${response.status})`
+}), {
status: response.status,
headers: {
'Content-Type': 'application/json',
},
}) 📝 Committable suggestion
Suggested change
|
||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||
const data = await response.json() | ||||||||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add error handling for successful response parsing. The JSON parsing of the successful response should also be wrapped in a try-catch block to prevent unhandled exceptions. -const data = await response.json()
+let data;
+try {
+ data = await response.json()
+} catch (e) {
+ console.error('Error parsing successful response:', e)
+ return new NextResponse('Internal Server Error', { status: 500 })
+} 📝 Committable suggestion
Suggested change
|
||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||
return new NextResponse(JSON.stringify(data), { | ||||||||||||||||||||||||||||||||
status: 200, | ||||||||||||||||||||||||||||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🛠️ Refactor suggestion
Enhance error handling to provide more informative responses.
While the try-catch block for JSON parsing is good, the error handling could be more informative: