-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #28 from webzard-io/fix/http_error_handle
fix: data api's get one throw error when get empty data and transform http error
- Loading branch information
Showing
3 changed files
with
201 additions
and
95 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import { transformHttpError } from '../../src/utils/transform-http-error'; | ||
|
||
describe('transformHttpError', () => { | ||
it('should return an HttpError object with statusText and status code if error has response', () => { | ||
const error = { | ||
response: { | ||
statusText: 'Not Found', | ||
status: 404, | ||
}, | ||
}; | ||
|
||
const expected = { | ||
...error, | ||
message: 'Not Found', | ||
statusCode: 404, | ||
}; | ||
|
||
expect(transformHttpError(error)).toEqual(expected); | ||
}); | ||
|
||
it('should return an HttpError object with error message, undefined statusCode if error does not have response', () => { | ||
const error = new Error('Internal Server Error'); | ||
|
||
const expected = { | ||
error: error, | ||
message: 'Internal Server Error', | ||
statusCode: undefined, | ||
}; | ||
|
||
expect(transformHttpError(error)).toEqual(expected); | ||
}); | ||
|
||
it('should return an HttpError object with error message as JSON string if error is an object', () => { | ||
const error = { message: 'Bad Request' }; | ||
|
||
const expected = { | ||
error: error, | ||
message: '{"message":"Bad Request"}', | ||
statusCode: undefined, | ||
}; | ||
|
||
expect(transformHttpError(error)).toEqual(expected); | ||
}); | ||
|
||
it('should return an HttpError object with error message as string if error is not an object', () => { | ||
const error = 'Unauthorized'; | ||
|
||
const expected = { | ||
error: error, | ||
message: 'Unauthorized', | ||
statusCode: undefined, | ||
}; | ||
|
||
expect(transformHttpError(error)).toEqual(expected); | ||
}); | ||
}); |
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,21 @@ | ||
import { HttpError } from '@refinedev/core'; | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
export const transformHttpError = (error: any): HttpError => { | ||
if (error?.response) { | ||
return { | ||
...error, | ||
message: error.response?.statusText, | ||
statusCode: error.response?.status, | ||
}; | ||
} | ||
return { | ||
error: error, | ||
message: | ||
typeof error === 'object' | ||
? error instanceof Error | ||
? error.message | ||
: JSON.stringify(error) | ||
: String(error), | ||
statusCode: undefined as unknown as number, | ||
}; | ||
}; |