Skip to content

Commit

Permalink
Merge pull request #3 from outpace/css+mf/add-full-result-to-response
Browse files Browse the repository at this point in the history
Update failed task with full error details
  • Loading branch information
omid-poorali authored Jul 9, 2022
2 parents f6a314f + e93cbed commit b903e33
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 5 deletions.
11 changes: 7 additions & 4 deletions src/hooks/use-uploader.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as React from 'react';
import * as Utils from '../utils';
import axios from 'axios';
import axios, { AxiosError } from 'axios';
import { Failed, Stopped, Task, Uploaded, Uploader, Uploading, UploadParams } from '../types';

type AbortMap = {
Expand Down Expand Up @@ -82,25 +82,28 @@ export function useUploader<Result = any>({
else form.append(`${fieldname}`, source);


const res = await axios.request({
const res = await axios.request<Result>({
...defaultConfig,
...{ url, method, headers, data: form },
})

updateTask(id, {
status: Uploaded, result: {
httpStatus: res.status,
responseData: res.data as Result
responseData: res.data
}
});

} catch (error) {
const { response }: any = error;
if (response) {
const errorResult = axios.isAxiosError(error) ? error as AxiosError<Result> : error as Error;

updateTask(id, {
status: Failed, result: {
httpStatus: response?.status,
responseData: response?.data as Result
responseData: response?.data,
error: errorResult
}
});
}
Expand Down
3 changes: 2 additions & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { AxiosRequestHeaders, Method } from "axios";
import { AxiosError, AxiosRequestHeaders, Method } from "axios";

export const Uploading = "uploading";
export const Stopped = "stopped";
Expand All @@ -17,6 +17,7 @@ export type UploadParams = {
export type TaskResult<A = any> = {
httpStatus: number | undefined;
responseData: A | undefined;
error?: AxiosError<A> | Error | undefined;
}

export type Task<A = any> = {
Expand Down
9 changes: 9 additions & 0 deletions tests/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ it('sucessfully uploaded a file', async () => {
}
});

expect(result.current[0][0].result?.error).toBeUndefined();
});

it('stop uploading process', async () => {
Expand Down Expand Up @@ -155,6 +156,14 @@ it('uploaded failed', async () => {
expect(result.current[0][0].size).toEqual(12)
expect(result.current[0][0].status).toEqual(Failed);
expect(result.current[0][0].result).toEqual({
error: {
response: {
data: {
message: "unauthorized",
},
status: 401,
},
},
httpStatus: 401,
responseData: {
message: 'unauthorized'
Expand Down

0 comments on commit b903e33

Please sign in to comment.