Skip to content

Commit e612885

Browse files
authored
fix(storage-browser): update task statuses before passing to callbacks, add error to TaskResult (#6484)
1 parent 5ea29c4 commit e612885

File tree

17 files changed

+201
-83
lines changed

17 files changed

+201
-83
lines changed

.changeset/forty-hotels-tan.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@aws-amplify/ui-react-storage": patch
3+
---
4+
5+
fix(storage-browser): update task statuses before passing to callbacks, add error to TaskResult

docs/src/pages/[platform]/connected-components/storage/storage-browser/react.mdx

Lines changed: 2 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -487,39 +487,12 @@ The following `default` actions are invoked by the default UI views:
487487

488488
<Example>
489489
<ExampleCode>
490-
```ts
491-
import { createStorageBrowser, defaultActionConfigs } from '@aws-amplify/ui-react-storage/browser';
492-
493-
const { StorageBrowser } = createStorageBrowser({
494-
actions: {
495-
default: {
496-
upload: {
497-
...defaultActionConfigs.upload,
498-
handler: ({ config, data, options }) =>
499-
defaultActionConfigs.upload.handler({
500-
config,
501-
data,
502-
options: {
503-
...options,
504-
onSuccess: (...input) => {
505-
options?.onSuccess?.(...input);
506-
fileUploadCounter.add(1); // Your custom counter metrics.
507-
},
508-
onError: (data, message, error) => {
509-
options?.onError?.(data, message, error);
510-
fileUploadError.recordError(error); // Your custom error metrics.
511-
},
512-
},
513-
}),
514-
}
515-
}
516-
}
517-
});
490+
```tsx file=../../../../../../../examples/next/pages/ui/components/storage/storage-browser/default-auth/routed/StorageBrowser.ts
518491
```
519492
</ExampleCode>
520493
</Example>
521494

522-
#### New custom actions
495+
#### Add Custom actions
523496

524497
`custom` actions can be provided to add entirely new functionality to `StorageBrowser` which can be used by custom UI views.
525498

examples/next/pages/ui/components/storage/storage-browser/default-auth/routed/StorageBrowser.ts

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,54 @@ import { Amplify } from 'aws-amplify';
33
import {
44
createAmplifyAuthAdapter,
55
createStorageBrowser,
6+
defaultHandlers,
7+
defaultActionConfigs,
68
} from '@aws-amplify/ui-react-storage/browser';
79
import '@aws-amplify/ui-react-storage/styles.css';
810

911
import config from './aws-exports';
1012

1113
Amplify.configure(config);
1214

15+
const uploads = [];
16+
const errors = [];
17+
const fileUploadCounter = {
18+
add: (value: { key: string }) => {
19+
uploads.push(value);
20+
21+
console.log('uploads', uploads);
22+
},
23+
recordError: (error: Error) => {
24+
errors.push(error);
25+
26+
console.log('errors', errors);
27+
},
28+
};
29+
1330
export const { StorageBrowser } = createStorageBrowser({
1431
config: createAmplifyAuthAdapter(),
32+
actions: {
33+
default: {
34+
upload: {
35+
...defaultActionConfigs.upload,
36+
handler: (input) => {
37+
const output = defaultHandlers.upload(input);
38+
39+
output.result.then((result) => {
40+
const { error, status, value } = result;
41+
if (status === 'COMPLETE') {
42+
fileUploadCounter.add(value);
43+
} else if (
44+
status === 'FAILED' ||
45+
status === 'OVERWRITE_PREVENTED'
46+
) {
47+
fileUploadCounter.recordError(error);
48+
}
49+
});
50+
51+
return output;
52+
},
53+
},
54+
},
55+
},
1556
});

packages/react-storage/src/components/StorageBrowser/actions/handlers/__tests__/copy.spec.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -113,13 +113,15 @@ describe('copyHandler', () => {
113113
});
114114

115115
it('returns failed status', async () => {
116-
const errorMessage = 'error-message';
117-
mockCopy.mockRejectedValue(new Error(errorMessage));
116+
const error = new Error('No copy!');
117+
118+
mockCopy.mockRejectedValue(error);
118119
const { result } = copyHandler(baseInput);
119120

120121
expect(await result).toEqual({
122+
error,
123+
message: error.message,
121124
status: 'FAILED',
122-
message: errorMessage,
123125
});
124126
});
125127
});

packages/react-storage/src/components/StorageBrowser/actions/handlers/__tests__/createFolder.spec.ts

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -122,37 +122,38 @@ describe('createFolderHandler', () => {
122122
});
123123

124124
it('handles a failure as expected', async () => {
125-
const errorMessage = 'error-message';
125+
const error = new Error('No new folder!');
126126

127127
mockUploadData.mockReturnValue({
128128
...mockUploadDataReturnValue,
129-
result: Promise.reject(new Error(errorMessage)),
129+
result: Promise.reject(error),
130130
state: 'ERROR',
131131
});
132132

133133
const { result } = createFolderHandler(baseInput);
134134

135135
expect(await result).toStrictEqual({
136-
message: errorMessage,
136+
error,
137+
message: error.message,
137138
status: 'FAILED',
138139
});
139140
});
140141

141142
it('returns "OVERWRITE_PREVENTED" on `PreconditionFailed` error', async () => {
142-
const message = 'No overwrite!';
143-
const overwritePreventedError = new Error(message);
144-
overwritePreventedError.name = 'PreconditionFailed';
143+
const error = new Error('No overwrite!');
144+
error.name = 'PreconditionFailed';
145145

146146
mockUploadData.mockReturnValue({
147147
...mockUploadDataReturnValue,
148-
result: Promise.reject(overwritePreventedError),
148+
result: Promise.reject(error),
149149
state: 'ERROR',
150150
});
151151

152152
const { result } = createFolderHandler(baseInput);
153153

154154
expect(await result).toStrictEqual({
155-
message,
155+
error,
156+
message: error.message,
156157
status: 'OVERWRITE_PREVENTED',
157158
});
158159
});

packages/react-storage/src/components/StorageBrowser/actions/handlers/__tests__/delete.spec.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,13 +61,14 @@ describe('deleteHandler', () => {
6161
});
6262

6363
it('returns failed status', async () => {
64-
const errorMessage = 'error-message';
65-
mockRemove.mockRejectedValue(new Error(errorMessage));
64+
const error = new Error('No delete!');
65+
mockRemove.mockRejectedValue(error);
6666
const { result } = deleteHandler(baseInput);
6767

6868
expect(await result).toEqual({
69+
error,
70+
message: error.message,
6971
status: 'FAILED',
70-
message: errorMessage,
7172
});
7273
});
7374
});

packages/react-storage/src/components/StorageBrowser/actions/handlers/__tests__/download.spec.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,13 +61,14 @@ describe('downloadHandler', () => {
6161
});
6262

6363
it('returns failed status', async () => {
64-
const errorMessage = 'error-message';
65-
mockGetUrl.mockRejectedValue(new Error(errorMessage));
64+
const error = new Error('No download!');
65+
mockGetUrl.mockRejectedValue(error);
6666
const { result } = downloadHandler(baseInput);
6767

6868
expect(await result).toEqual({
69+
error,
70+
message: error.message,
6971
status: 'FAILED',
70-
message: errorMessage,
7172
});
7273
});
7374
});

packages/react-storage/src/components/StorageBrowser/actions/handlers/__tests__/upload.spec.ts

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,7 @@ describe('uploadHandler', () => {
178178
const { result } = uploadHandler(baseInput);
179179

180180
expect(await result).toStrictEqual({
181+
error,
181182
message: error.message,
182183
status: 'FAILED',
183184
});
@@ -196,18 +197,19 @@ describe('uploadHandler', () => {
196197
const { result } = uploadHandler(baseInput);
197198

198199
expect(await result).toStrictEqual({
199-
message: 'Failed!',
200+
error,
201+
message: error.message,
200202
status: 'CANCELED',
201203
});
202204
});
203205

204206
it('handles an overwrite failure as expected', async () => {
205-
const preconditionError = new Error('Failed!');
206-
preconditionError.name = 'PreconditionFailed';
207+
const error = new Error('Failed!');
208+
error.name = 'PreconditionFailed';
207209

208210
mockUploadData.mockReturnValue({
209211
...mockUploadDataReturnValue,
210-
result: Promise.reject(preconditionError),
212+
result: Promise.reject(error),
211213
state: 'ERROR',
212214
});
213215

@@ -217,7 +219,8 @@ describe('uploadHandler', () => {
217219
});
218220

219221
expect(await result).toStrictEqual({
220-
message: 'Failed!',
222+
error,
223+
message: error.message,
221224
status: 'OVERWRITE_PREVENTED',
222225
});
223226
});

packages/react-storage/src/components/StorageBrowser/actions/handlers/copy.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,9 @@ export const copyHandler: CopyHandler = (input) => {
6767
status: 'COMPLETE' as const,
6868
value: { key: path },
6969
}))
70-
.catch(({ message }: Error) => ({ message, status: 'FAILED' })),
70+
.catch((error: Error) => {
71+
const { message } = error;
72+
return { error, message, status: 'FAILED' };
73+
}),
7174
};
7275
};

packages/react-storage/src/components/StorageBrowser/actions/handlers/createFolder.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,11 +58,12 @@ export const createFolderHandler: CreateFolderHandler = (input) => {
5858
status: 'COMPLETE' as const,
5959
value: { key: path },
6060
}))
61-
.catch(({ message, name }: Error) => {
61+
.catch((error: Error) => {
62+
const { message, name } = error;
6263
if (name === 'PreconditionFailed') {
63-
return { message, status: 'OVERWRITE_PREVENTED' };
64+
return { error, message, status: 'OVERWRITE_PREVENTED' };
6465
}
65-
return { message, status: 'FAILED' };
66+
return { error, message, status: 'FAILED' };
6667
}),
6768
};
6869
};

0 commit comments

Comments
 (0)