Skip to content

Commit

Permalink
fix: address feedbacks
Browse files Browse the repository at this point in the history
Co-authored-by: Hui Zhao <[email protected]>
  • Loading branch information
AllanZhengYP and HuiSF committed Jan 16, 2024
1 parent d2aede5 commit 2bf22dd
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 40 deletions.
4 changes: 0 additions & 4 deletions packages/api-rest/src/errors/RestApiError.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,5 @@ import { ApiError, ApiErrorParams } from '@aws-amplify/core/internals/utils';
export class RestApiError extends ApiError {
constructor(params: ApiErrorParams) {
super(params);

// TODO: Delete the following 2 lines after we change the build target to >= es2015
this.constructor = RestApiError;
Object.setPrototypeOf(this, RestApiError.prototype);
}
}
71 changes: 40 additions & 31 deletions packages/api-rest/src/utils/serviceError.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,7 @@ export const parseRestApiServiceError = async (
// Response is not considered an error.
return;
} else {
let bodyText: string | undefined = undefined;
try {
bodyText = await response.body?.text();
} catch (error) {
// SKIP: error response may not have a body
}
const bodyText = await response.body?.text();
return buildRestApiError(parsedAwsError, {
statusCode: response.statusCode,
headers: response.headers,
Expand All @@ -52,31 +47,45 @@ export const parseRestApiServiceError = async (
*/
const stubErrorResponse = (response: HttpResponse): HttpResponse => {
let bodyTextPromise: Promise<string> | undefined = undefined;
return {
...response,
body: {
json: async () => {
if (!bodyTextPromise) {
bodyTextPromise = response.body?.text();
}
try {
return JSON.parse(await bodyTextPromise!);
} catch (error) {
// If response body is not a valid JSON, we stub it to be an empty object and eventually parsed as
// an unknown error
return {};
}
},
blob: async () => {},
// For non-AWS errors, users can access the body as a string as a fallback.
text: async () => {
if (!bodyTextPromise) {
bodyTextPromise = response.body?.text();
}
return bodyTextPromise;
},
} as any,
};
const bodyProxy = new Proxy(response.body, {
get(target, prop, receiver) {
if (prop === 'json') {
// For potential AWS errors, error parser will try to parse the body as JSON first.
return async () => {
if (!bodyTextPromise) {
bodyTextPromise = target.text();
}
try {
return JSON.parse(await bodyTextPromise!);
} catch (error) {
// If response body is not a valid JSON, we stub it to be an empty object and eventually parsed
// as an unknown error
return {};
}
};
} else if (prop === 'text') {
// For non-AWS errors, users can access the body as a string as a fallback.
return async () => {
if (!bodyTextPromise) {
bodyTextPromise = target.text();
}
return bodyTextPromise;
};
} else {
return Reflect.get(target, prop, receiver);
}
},
});
const responseProxy = new Proxy(response, {
get(target, prop, receiver) {
if (prop === 'body') {
return bodyProxy;
} else {
return Reflect.get(target, prop, receiver);
}
},
});
return responseProxy;
};

/**
Expand Down
6 changes: 1 addition & 5 deletions packages/core/src/errors/APIError.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export class ApiError extends AmplifyError {
/**
* The unwrapped HTTP response causing the given API error.
*/
public get response(): ApiErrorResponse | undefined {
get response(): ApiErrorResponse | undefined {
return this._response
? replicateApiErrorResponse(this._response)
: undefined;
Expand All @@ -41,10 +41,6 @@ export class ApiError extends AmplifyError {
constructor(params: ApiErrorParams) {
super(params);

// TODO: Delete the following 2 lines after we change the build target to >= es2015
this.constructor = ApiError;
Object.setPrototypeOf(this, ApiError.prototype);

if (params.response) {
this._response = params.response;
}
Expand Down

0 comments on commit 2bf22dd

Please sign in to comment.