1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
5 import { AxiosInstance } from "axios";
6 import { snakeCase } from "lodash";
7 import { Resource } from "models/resource";
8 import { ApiActions } from "services/api/api-actions";
9 import { CommonService } from "services/common-service/common-service";
11 export enum CommonResourceServiceError {
12 UNIQUE_NAME_VIOLATION = 'UniqueNameViolation',
13 OWNERSHIP_CYCLE = 'OwnershipCycle',
14 MODIFYING_CONTAINER_REQUEST_FINAL_STATE = 'ModifyingContainerRequestFinalState',
15 NAME_HAS_ALREADY_BEEN_TAKEN = 'NameHasAlreadyBeenTaken',
16 PERMISSION_ERROR_FORBIDDEN = 'PermissionErrorForbidden',
21 export class CommonResourceService<T extends Resource> extends CommonService<T> {
22 constructor(serverApi: AxiosInstance, resourceType: string, actions: ApiActions, readOnlyFields: string[] = []) {
23 super(serverApi, resourceType, actions, readOnlyFields.concat([
30 create(data?: Partial<T>, showErrors?: boolean) {
32 if (data !== undefined) {
33 this.readOnlyFields.forEach( field => delete data[field] );
35 [this.resourceType.slice(0, -1)]: CommonService.mapKeys(snakeCase)(data),
38 return super.create(payload, showErrors);
41 update(uuid: string, data: Partial<T>, showErrors?: boolean, select?: string[]) {
43 if (data !== undefined) {
44 this.readOnlyFields.forEach( field => delete data[field] );
46 [this.resourceType.slice(0, -1)]: CommonService.mapKeys(snakeCase)(data),
48 if (select !== undefined && select.length > 0) {
49 payload.select = ['uuid', ...select.map(field => snakeCase(field))];
52 return super.update(uuid, payload, showErrors);
56 export const getCommonResourceServiceError = (errorResponse: any) => {
57 if ('errors' in errorResponse) {
58 const error = errorResponse.errors.join('');
60 case /UniqueViolation/.test(error):
61 return CommonResourceServiceError.UNIQUE_NAME_VIOLATION;
62 case /ownership cycle/.test(error):
63 return CommonResourceServiceError.OWNERSHIP_CYCLE;
64 case /Mounts cannot be modified in state 'Final'/.test(error):
65 return CommonResourceServiceError.MODIFYING_CONTAINER_REQUEST_FINAL_STATE;
66 case /Name has already been taken/.test(error):
67 return CommonResourceServiceError.NAME_HAS_ALREADY_BEEN_TAKEN;
68 case /403 Forbidden/.test(error):
69 return CommonResourceServiceError.PERMISSION_ERROR_FORBIDDEN;
71 return CommonResourceServiceError.UNKNOWN;
74 return CommonResourceServiceError.NONE;