Skip to content

Commit

Permalink
Merge branch 'next' into next-api-v6
Browse files Browse the repository at this point in the history
  • Loading branch information
david-mcafee committed Aug 31, 2023
2 parents c2912f0 + fbac776 commit daeac44
Show file tree
Hide file tree
Showing 17 changed files with 61 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ describe('S3 APIs functional test', () => {
if (caseType === 'happy case') {
fail(`${name} ${caseType} should succeed: ${e}`);
} else {
expect(e).toBeInstanceOf(Error);
expect(e).toBeInstanceOf(StorageError);
expect(e).toEqual(expect.objectContaining(outputOrError));
}
}
Expand Down
13 changes: 1 addition & 12 deletions packages/storage/src/errors/StorageError.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,8 @@
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
import { AmplifyError, ErrorParams, ServiceError } from '@aws-amplify/core/internals/utils';
import { AmplifyError, ErrorParams } from '@aws-amplify/core/internals/utils';

export class StorageError extends AmplifyError {
static fromServiceError(error: Error, statusCode: number): ServiceError {
const storageError = new StorageError({
name: error.name,
message: error.message,
});
if (statusCode === 404) {
storageError.recoverySuggestion =
'Please add the object with this key to the bucket as the key is not found.';
}
throw storageError;
}
constructor(params: ErrorParams) {
super(params);

Expand Down
1 change: 1 addition & 0 deletions packages/storage/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,4 @@ export {
} from './providers/s3';
// TODO[AllanZhengYP]: support isCancelError in Node.js with node-fetch
export { isCancelError } from './errors/CanceledError';
export { StorageError } from './errors/StorageError';
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,11 @@ import type { AbortMultipartUploadCommandInput } from './types';

import { defaultConfig } from './base';
import {
validateS3RequiredParameter,
buildStorageServiceError,
parseXmlError,
s3TransferHandler,
serializePathnameObjectKey,
validateS3RequiredParameter,
} from './utils';

export type AbortMultipartUploadInput = Pick<
Expand Down Expand Up @@ -48,8 +49,8 @@ const abortMultipartUploadDeserializer = async (
response: HttpResponse
): Promise<AbortMultipartUploadOutput> => {
if (response.statusCode >= 300) {
const error = await parseXmlError(response);
throw error;
const error = (await parseXmlError(response)) as Error;
throw buildStorageServiceError(error, response.statusCode);
} else {
return {
$metadata: parseMetadata(response),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import type {
} from './types';
import { defaultConfig } from './base';
import {
buildStorageServiceError,
map,
parseXmlBody,
parseXmlError,
Expand Down Expand Up @@ -87,12 +88,11 @@ const serializeCompletedPartList = (input: CompletedPart): string => {
const parseXmlBodyOrThrow = async (response: HttpResponse): Promise<any> => {
const parsed = await parseXmlBody(response); // Handles empty body case
if (parsed.Code !== undefined && parsed.Message !== undefined) {
const error = await parseXmlError({
const error = (await parseXmlError({
...response,
statusCode: 500, // To workaround the >=300 status code check common to other APIs.
});
error!.$metadata.httpStatusCode = response.statusCode;
throw error;
})) as Error;
throw buildStorageServiceError(error, response.statusCode);
}
return parsed;
};
Expand All @@ -101,8 +101,8 @@ const completeMultipartUploadDeserializer = async (
response: HttpResponse
): Promise<CompleteMultipartUploadOutput> => {
if (response.statusCode >= 300) {
const error = await parseXmlError(response);
throw error;
const error = (await parseXmlError(response)) as Error;
throw buildStorageServiceError(error, response.statusCode);
} else {
const parsed = await parseXmlBodyOrThrow(response);
const contents = map(parsed, {
Expand Down
5 changes: 2 additions & 3 deletions packages/storage/src/providers/s3/utils/client/copyObject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ import {
import { composeServiceApi } from '@aws-amplify/core/internals/aws-client-utils/composers';
import type { CopyObjectCommandInput, CopyObjectCommandOutput } from './types';
import { defaultConfig } from './base';
import { StorageError } from '../../../../errors/StorageError';
import {
buildStorageServiceError,
parseXmlBody,
parseXmlError,
s3TransferHandler,
Expand Down Expand Up @@ -64,9 +64,8 @@ const copyObjectDeserializer = async (
response: HttpResponse
): Promise<CopyObjectOutput> => {
if (response.statusCode >= 300) {
// error is always set when statusCode >= 300
const error = (await parseXmlError(response)) as Error;
throw StorageError.fromServiceError(error, response.statusCode);
throw buildStorageServiceError(error, response.statusCode);
} else {
await parseXmlBody(response);
return {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import type { PutObjectInput } from './putObject';

import { defaultConfig } from './base';
import {
buildStorageServiceError,
validateS3RequiredParameter,
map,
parseXmlBody,
Expand Down Expand Up @@ -55,8 +56,8 @@ const createMultipartUploadDeserializer = async (
response: HttpResponse
): Promise<CreateMultipartUploadOutput> => {
if (response.statusCode >= 300) {
const error = await parseXmlError(response);
throw error;
const error = (await parseXmlError(response)) as Error;
throw buildStorageServiceError(error, response.statusCode);
} else {
const parsed = await parseXmlBody(response);
const contents = map(parsed, {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import type {

import { defaultConfig } from './base';
import {
buildStorageServiceError,
deserializeBoolean,
map,
parseXmlError,
Expand Down Expand Up @@ -51,7 +52,7 @@ const deleteObjectDeserializer = async (
if (response.statusCode >= 300) {
// error is always set when statusCode >= 300
const error = (await parseXmlError(response)) as Error;
throw StorageError.fromServiceError(error, response.statusCode);
throw buildStorageServiceError(error, response.statusCode);
} else {
const content = map(response.headers, {
DeleteMarker: ['x-amz-delete-marker', deserializeBoolean],
Expand Down
5 changes: 3 additions & 2 deletions packages/storage/src/providers/s3/utils/client/getObject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import type {
GetObjectCommandOutput,
} from './types';
import {
buildStorageServiceError,
deserializeBoolean,
deserializeMetadata,
deserializeNumber,
Expand Down Expand Up @@ -56,8 +57,8 @@ const getObjectDeserializer = async (
response: HttpResponse
): Promise<GetObjectOutput> => {
if (response.statusCode >= 300) {
const error = await parseXmlError(response);
throw error;
const error = (await parseXmlError(response)) as Error;
throw buildStorageServiceError(error, response.statusCode);
} else if (!response.body) {
throw new Error('Got empty response body.');
} else {
Expand Down
3 changes: 2 additions & 1 deletion packages/storage/src/providers/s3/utils/client/headObject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { defaultConfig } from './base';
import type { HeadObjectCommandInput, HeadObjectCommandOutput } from './types';

import {
buildStorageServiceError,
deserializeMetadata,
deserializeNumber,
deserializeTimestamp,
Expand Down Expand Up @@ -56,7 +57,7 @@ const headObjectDeserializer = async (
if (response.statusCode >= 300) {
// error is always set when statusCode >= 300
const error = (await parseXmlError(response)) as Error;
throw StorageError.fromServiceError(error, response.statusCode);
throw buildStorageServiceError(error, response.statusCode);
} else {
const contents = {
...map(response.headers, {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import type {
import { defaultConfig } from './base';
import {
assignStringVariables,
buildStorageServiceError,
deserializeBoolean,
deserializeNumber,
deserializeTimestamp,
Expand All @@ -24,7 +25,6 @@ import {
parseXmlError,
s3TransferHandler,
} from './utils';
import { StorageError } from '../../../../errors/StorageError';

export type ListObjectsV2Input = ListObjectsV2CommandInput;

Expand Down Expand Up @@ -63,7 +63,7 @@ const listObjectsV2Deserializer = async (
if (response.statusCode >= 300) {
// error is always set when statusCode >= 300
const error = (await parseXmlError(response)) as Error;
throw StorageError.fromServiceError(error, response.statusCode);
throw buildStorageServiceError(error, response.statusCode);
} else {
const parsed = await parseXmlBody(response);
const contents = map(parsed, {
Expand Down
5 changes: 3 additions & 2 deletions packages/storage/src/providers/s3/utils/client/listParts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import type {
} from './types';
import { defaultConfig } from './base';
import {
buildStorageServiceError,
emptyArrayGuard,
map,
parseXmlBody,
Expand Down Expand Up @@ -58,8 +59,8 @@ const listPartsDeserializer = async (
response: HttpResponse
): Promise<ListPartsOutput> => {
if (response.statusCode >= 300) {
const error = await parseXmlError(response);
throw error;
const error = (await parseXmlError(response)) as Error;
throw buildStorageServiceError(error, response.statusCode);
} else {
const parsed = await parseXmlBody(response);
const contents = map(parsed, {
Expand Down
5 changes: 3 additions & 2 deletions packages/storage/src/providers/s3/utils/client/putObject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { composeServiceApi } from '@aws-amplify/core/internals/aws-client-utils/
import { defaultConfig } from './base';
import type { PutObjectCommandInput, PutObjectCommandOutput } from './types';
import {
buildStorageServiceError,
validateS3RequiredParameter,
assignStringVariables,
map,
Expand Down Expand Up @@ -70,8 +71,8 @@ const putObjectDeserializer = async (
response: HttpResponse
): Promise<PutObjectOutput> => {
if (response.statusCode >= 300) {
const error = await parseXmlError(response);
throw error;
const error = (await parseXmlError(response)) as Error;
throw buildStorageServiceError(error, response.statusCode);
} else {
return {
...map(response.headers, {
Expand Down
5 changes: 3 additions & 2 deletions packages/storage/src/providers/s3/utils/client/uploadPart.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { composeServiceApi } from '@aws-amplify/core/internals/aws-client-utils/
import { defaultConfig } from './base';
import type { UploadPartCommandInput, UploadPartCommandOutput } from './types';
import {
buildStorageServiceError,
validateS3RequiredParameter,
assignStringVariables,
map,
Expand Down Expand Up @@ -61,8 +62,8 @@ const uploadPartDeserializer = async (
response: HttpResponse
): Promise<UploadPartOutput> => {
if (response.statusCode >= 300) {
const error = await parseXmlError(response);
throw error;
const error = (await parseXmlError(response)) as Error;
throw buildStorageServiceError(error, response.statusCode);
} else {
return {
...map(response.headers, {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
// SPDX-License-Identifier: Apache-2.0

import { Headers } from '@aws-amplify/core/internals/aws-client-utils';
import { ServiceError } from '@aws-amplify/core/internals/utils';

import { StorageError } from '../../../../../errors/StorageError';

type PropertyNameWithStringValue = string;
type PropertyNameWithSubsequentDeserializer<T> = [string, (arg: any) => T];
Expand Down Expand Up @@ -133,3 +136,23 @@ export const deserializeMetadata = (
}, {} as any);
return Object.keys(deserialized).length > 0 ? deserialized : undefined;
};

/**
* Internal-only method to create a new StorageError from a service error.
*
* @internal
*/
export const buildStorageServiceError = (
error: Error,
statusCode: number
): ServiceError => {
const storageError = new StorageError({
name: error.name,
message: error.message,
});
if (statusCode === 404) {
storageError.recoverySuggestion =
'Please add the object with this key to the bucket as the key is not found.';
}
return storageError;
};
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export {
utf8Encode,
} from '../runtime';
export {
buildStorageServiceError,
deserializeBoolean,
deserializeMetadata,
deserializeNumber,
Expand Down
2 changes: 1 addition & 1 deletion packages/storage/src/types/params.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export type StorageOptions =
| { accessLevel?: 'guest' | 'private' }
| {
accessLevel: 'protected';
targetIdentityId: string;
targetIdentityId?: string;
};

export type StorageOperationRequest<Options extends StorageOptions> = {
Expand Down

0 comments on commit daeac44

Please sign in to comment.