Adjusted the error mechanism to better follow the standard practice of rejecting HTTP errors

This commit is contained in:
Christopher Allford 2020-10-02 14:10:24 -07:00
parent 54b876fc75
commit b74adf215a
6 changed files with 6 additions and 23 deletions

View File

@ -51,6 +51,8 @@ httpClient.get( '/wc/v3/products' ).then( ( response ) => {
response.headers;
// Access the data from the response, in this case, the products.
response.data;
}, ( error ) => {
// Handle errors that may have come up.
} );
```

View File

@ -49,9 +49,7 @@ describe( 'AxiosResponseInterceptor', () => {
responseText: JSON.stringify( { code: 'error_code', message: 'value' } ),
} );
const response = await axiosInstance.get( 'http://test.test' );
expect( response ).toMatchObject( {
await expect( axiosInstance.get( 'http://test.test' ) ).rejects.toMatchObject( {
statusCode: 404,
headers: {
'content-type': 'application/json',

View File

@ -21,14 +21,12 @@ export class AxiosResponseInterceptor extends AxiosInterceptor {
* @param {*} error The error that was caught.
* @return {Promise} A promise containing the HTTPResponse.
*/
protected onResponseRejected( error: any ): Promise< HTTPResponse > {
protected onResponseRejected( error: any ): Promise< any > {
// Convert HTTP response errors into a form that we can handle them with.
if ( error.response ) {
return Promise.resolve< HTTPResponse >(
new HTTPResponse( error.response.status, error.response.headers, error.response.data ),
);
return Promise.reject( new HTTPResponse( error.response.status, error.response.headers, error.response.data ) );
}
throw error;
return Promise.reject( error );
}
}

View File

@ -13,9 +13,6 @@ function restCreate( httpClient: HTTPClient ): CreateFn< SimpleProductRepository
regular_price: properties.regularPrice,
},
);
if ( response.statusCode >= 400 ) {
throw response;
}
return Promise.resolve( new SimpleProduct( {
id: response.data.id,

View File

@ -6,9 +6,6 @@ import { ListsSettingGroups, SettingGroupRepositoryParams } from '../../../model
function restList( httpClient: HTTPClient ): ListFn< SettingGroupRepositoryParams > {
return async () => {
const response = await httpClient.get( '/wc/v3/settings' );
if ( response.statusCode >= 400 ) {
throw response;
}
const list: SettingGroup[] = [];
for ( const raw of response.data ) {

View File

@ -16,9 +16,6 @@ import {
function restList( httpClient: HTTPClient ): ListChildFn< SettingRepositoryParams > {
return async ( parent ) => {
const response = await httpClient.get( '/wc/v3/settings/' + parent );
if ( response.statusCode >= 400 ) {
throw response;
}
const list: Setting[] = [];
for ( const raw of response.data ) {
@ -40,9 +37,6 @@ function restList( httpClient: HTTPClient ): ListChildFn< SettingRepositoryParam
function restRead( httpClient: HTTPClient ): ReadChildFn< SettingRepositoryParams > {
return async ( parent, id ) => {
const response = await httpClient.get( '/wc/v3/settings/' + parent + '/' + id );
if ( response.statusCode >= 400 ) {
throw response;
}
return Promise.resolve( new Setting( {
id: response.data.id,
@ -62,9 +56,6 @@ function restUpdate( httpClient: HTTPClient ): UpdateChildFn< SettingRepositoryP
'/wc/v3/settings/' + parent + '/' + id,
params,
);
if ( response.statusCode >= 400 ) {
throw response;
}
return Promise.resolve( new Setting( {
id: response.data.id,