-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(api): expose HTTP response from API errors (#12835)
--------- Co-authored-by: Hui Zhao <[email protected]> Co-authored-by: Jim Blanchard <[email protected]>
- Loading branch information
1 parent
9f41cdb
commit 38c1d56
Showing
14 changed files
with
355 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
export { APIError } from './APIError'; | ||
export { GraphQLApiError } from './GraphQLApiError'; | ||
export { assertValidationError } from './assertValidationError'; | ||
export { APIValidationErrorCode, validationErrorMap } from './validation'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
import { parseJsonError as parseAwsJsonError } from '@aws-amplify/core/internals/aws-client-utils'; | ||
|
||
import { parseRestApiServiceError } from '../../src/utils'; | ||
import { RestApiError } from '../../src/errors'; | ||
|
||
jest.mock('@aws-amplify/core/internals/aws-client-utils'); | ||
|
||
const mockParseJsonError = parseAwsJsonError as jest.Mock; | ||
const mockResponse = { | ||
statusCode: 400, | ||
headers: {}, | ||
body: { | ||
json: jest.fn(), | ||
blob: jest.fn(), | ||
text: jest.fn(), | ||
}, | ||
}; | ||
|
||
describe('parseRestApiServiceError', () => { | ||
beforeEach(() => { | ||
jest.resetAllMocks(); | ||
}); | ||
it('should return undefined if response is undefined', async () => { | ||
expect.assertions(1); | ||
expect(await parseRestApiServiceError(undefined)).toBeUndefined(); | ||
}); | ||
|
||
it('should return undefined if AWS JSON error parser returns undefined', async () => { | ||
expect.assertions(1); | ||
mockParseJsonError.mockReturnValue(undefined); | ||
expect(await parseRestApiServiceError(mockResponse)).toBeUndefined(); | ||
}); | ||
|
||
it('should return a RestApiError with the parsed AWS error', async () => { | ||
expect.assertions(2); | ||
const parsedAwsError = { | ||
name: 'UnknownError', | ||
message: 'Unknown error', | ||
$metadata: {}, | ||
}; | ||
mockParseJsonError.mockResolvedValue(parsedAwsError); | ||
const parsedRestApiError = await parseRestApiServiceError(mockResponse); | ||
expect(parsedRestApiError).toBeInstanceOf(RestApiError); | ||
expect(parsedRestApiError).toMatchObject(parsedAwsError); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.