X-Git-Url: https://git.arvados.org/arvados-workbench2.git/blobdiff_plain/7df4149100f41cc8d4a5438630ba95a1d72409a1..10ce16c28de952f6533ca3cc9df909269e3d2a53:/src/common/api/common-resource-service.ts diff --git a/src/common/api/common-resource-service.ts b/src/common/api/common-resource-service.ts index 2541feab..3956fb73 100644 --- a/src/common/api/common-resource-service.ts +++ b/src/common/api/common-resource-service.ts @@ -5,7 +5,7 @@ import * as _ from "lodash"; import { FilterBuilder } from "./filter-builder"; import { OrderBuilder } from "./order-builder"; -import { AxiosInstance } from "axios"; +import { AxiosInstance, AxiosPromise } from "axios"; import { Resource } from "../../models/resource"; export interface ListArguments { @@ -26,6 +26,11 @@ export interface ListResults { itemsAvailable: number; } +export interface Errors { + errors: string[]; + errorToken: string; +} + export class CommonResourceService { static mapResponseKeys = (response: any): Promise => @@ -49,6 +54,12 @@ export class CommonResourceService { } } + static defaultResponse(promise: AxiosPromise): Promise { + return promise + .then(CommonResourceService.mapResponseKeys) + .catch(({ response }) => Promise.reject(CommonResourceService.mapResponseKeys(response))); + } + protected serverApi: AxiosInstance; protected resourceType: string; @@ -58,21 +69,21 @@ export class CommonResourceService { } create(data: Partial) { - return this.serverApi - .post(this.resourceType, CommonResourceService.mapKeys(_.snakeCase)(data)) - .then(CommonResourceService.mapResponseKeys); + return CommonResourceService.defaultResponse( + this.serverApi + .post(this.resourceType, CommonResourceService.mapKeys(_.snakeCase)(data))); } delete(uuid: string): Promise { - return this.serverApi - .delete(this.resourceType + uuid) - .then(CommonResourceService.mapResponseKeys); + return CommonResourceService.defaultResponse( + this.serverApi + .delete(this.resourceType + uuid)); } get(uuid: string) { - return this.serverApi - .get(this.resourceType + uuid) - .then(CommonResourceService.mapResponseKeys); + return CommonResourceService.defaultResponse( + this.serverApi + .get(this.resourceType + uuid)); } list(args: ListArguments = {}): Promise> { @@ -82,11 +93,11 @@ export class CommonResourceService { filters: filters ? filters.serialize() : undefined, order: order ? order.getOrder() : undefined }; - return this.serverApi - .get(this.resourceType, { - params: CommonResourceService.mapKeys(_.snakeCase)(params) - }) - .then(CommonResourceService.mapResponseKeys); + return CommonResourceService.defaultResponse( + this.serverApi + .get(this.resourceType, { + params: CommonResourceService.mapKeys(_.snakeCase)(params) + })); } update(uuid: string) {