1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
5 import { AxiosInstance } from "axios";
6 import { Resource } from "src/models/resource";
7 import { ApiActions } from "~/services/api/api-actions";
8 import { CommonService } from "~/services/common-service/common-service";
10 export enum CommonResourceServiceError {
11 UNIQUE_NAME_VIOLATION = 'UniqueNameViolation',
12 OWNERSHIP_CYCLE = 'OwnershipCycle',
13 MODIFYING_CONTAINER_REQUEST_FINAL_STATE = 'ModifyingContainerRequestFinalState',
14 NAME_HAS_ALREADY_BEEN_TAKEN = 'NameHasAlreadyBeenTaken',
19 export class CommonResourceService<T extends Resource> extends CommonService<T> {
20 constructor(serverApi: AxiosInstance, resourceType: string, actions: ApiActions, readOnlyFields: string[] = []) {
21 super(serverApi, resourceType, actions, readOnlyFields.concat([
28 create(data?: Partial<T>) {
29 if (data !== undefined) {
30 this.readOnlyFields.forEach( field => delete data[field] );
32 return super.create(data);
35 update(uuid: string, data: Partial<T>) {
36 if (data !== undefined) {
37 this.readOnlyFields.forEach( field => delete data[field] );
39 return super.update(uuid, data);
43 export const getCommonResourceServiceError = (errorResponse: any) => {
44 if ('errors' in errorResponse && 'errorToken' in errorResponse) {
45 const error = errorResponse.errors.join('');
47 case /UniqueViolation/.test(error):
48 return CommonResourceServiceError.UNIQUE_NAME_VIOLATION;
49 case /ownership cycle/.test(error):
50 return CommonResourceServiceError.OWNERSHIP_CYCLE;
51 case /Mounts cannot be modified in state 'Final'/.test(error):
52 return CommonResourceServiceError.MODIFYING_CONTAINER_REQUEST_FINAL_STATE;
53 case /Name has already been taken/.test(error):
54 return CommonResourceServiceError.NAME_HAS_ALREADY_BEEN_TAKEN;
56 return CommonResourceServiceError.UNKNOWN;
59 return CommonResourceServiceError.NONE;