Skip to content

Commit af3302b

Browse files
responsebuffer
1 parent f58a8bf commit af3302b

File tree

3 files changed

+7
-27
lines changed

3 files changed

+7
-27
lines changed

packages/commerce-sdk-react/src/auth/index.test.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -989,8 +989,7 @@ describe('Auth', () => {
989989

990990
const mockErrorResponse = {
991991
status: 400,
992-
statusText: 'Bad Request',
993-
text: jest.fn().mockResolvedValue(JSON.stringify({message: 'Invalid request'}))
992+
json: jest.fn().mockResolvedValue({message: 'Invalid request'})
994993
}
995994
;(helpers.authorizePasswordless as jest.Mock).mockResolvedValueOnce(mockErrorResponse)
996995

@@ -1098,8 +1097,7 @@ describe('Auth', () => {
10981097

10991098
const mockErrorResponse = {
11001099
status: 400,
1101-
statusText: 'Bad Request',
1102-
text: jest.fn().mockResolvedValue(JSON.stringify({message: 'Invalid request'}))
1100+
json: jest.fn().mockResolvedValue({message: 'Invalid request'})
11031101
}
11041102
// @ts-expect-error private property
11051103
const getPasswordResetTokenSpy = jest.spyOn(auth.client, 'getPasswordResetToken')

packages/commerce-sdk-react/src/auth/index.ts

Lines changed: 4 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -111,26 +111,6 @@ type GetPasswordLessAccessTokenParams = {
111111
register_customer?: boolean | string
112112
}
113113

114-
/**
115-
* Throws an Error with status and message when response is not OK.
116-
* Uses res.text() to avoid SyntaxError on empty or non-JSON body (e.g. 404 from SLAS).
117-
*/
118-
async function throwIfResponseNotOk(
119-
res: {status: number; statusText?: string; text(): Promise<string>}
120-
): Promise<never> {
121-
let message = res.statusText || `${res.status}`
122-
try {
123-
const text = await res.text()
124-
if (text && text.trim()) {
125-
const errorData = JSON.parse(text)
126-
message = errorData?.message ?? message
127-
}
128-
} catch (_) {
129-
// Empty or non-JSON body (e.g. 404 from SLAS for user not found)
130-
}
131-
throw new Error(`${res.status} ${message}`)
132-
}
133-
134114
/**
135115
* The extended field is not from api response, we manually store the auth type,
136116
* so we don't need to make another API call when we already have the data.
@@ -1330,7 +1310,8 @@ class Auth {
13301310
}
13311311
})
13321312
if (res && res.status !== 200) {
1333-
await throwIfResponseNotOk(res)
1313+
const errorData = await res.json()
1314+
throw new Error(`${res.status} ${String(errorData.message)}`)
13341315
}
13351316
return res
13361317
}
@@ -1401,7 +1382,8 @@ class Auth {
14011382
// Set rawResponse to true to access the response body message for error handling
14021383
const res = await slasClient.getPasswordResetToken(options, true)
14031384
if (res && res.status !== 200) {
1404-
await throwIfResponseNotOk(res)
1385+
const errorData = await res.json()
1386+
throw new Error(`${res.status} ${String(errorData.message)}`)
14051387
}
14061388
return res
14071389
}

packages/pwa-kit-runtime/src/ssr/server/build-remote-server.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -988,7 +988,7 @@ export const RemoteServerFactory = {
988988
if (typeof options.onSLASPrivateProxyRes === 'function') {
989989
try {
990990
const customBuffer = options.onSLASPrivateProxyRes(
991-
workingBuffer,
991+
responseBuffer,
992992
proxyRes,
993993
req,
994994
res

0 commit comments

Comments
 (0)