Merge branch 'origin/master' into 14478-log-in-into-clusters
[arvados-workbench2.git] / src / services / common-service / common-resource-service.ts
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: AGPL-3.0
4
5 import * as _ from "lodash";
6 import { AxiosInstance } from "axios";
7 import { Resource } from "src/models/resource";
8 import { ApiActions } from "~/services/api/api-actions";
9 import { CommonService } from "~/services/common-service/common-service";
10
11 export enum CommonResourceServiceError {
12     UNIQUE_VIOLATION = 'UniqueViolation',
13     OWNERSHIP_CYCLE = 'OwnershipCycle',
14     MODIFYING_CONTAINER_REQUEST_FINAL_STATE = 'ModifyingContainerRequestFinalState',
15     NAME_HAS_ALREADY_BEEN_TAKEN = 'NameHasAlreadyBeenTaken',
16     UNKNOWN = 'Unknown',
17     NONE = 'None'
18 }
19
20 export class CommonResourceService<T extends Resource> extends CommonService<T> {
21
22     constructor(serverApi: AxiosInstance, resourceType: string, actions: ApiActions) {
23         super(serverApi, resourceType, actions);
24     }
25     
26 }
27
28 export const getCommonResourceServiceError = (errorResponse: any) => {
29     if ('errors' in errorResponse && 'errorToken' in errorResponse) {
30         const error = errorResponse.errors.join('');
31         switch (true) {
32             case /UniqueViolation/.test(error):
33                 return CommonResourceServiceError.UNIQUE_VIOLATION;
34             case /ownership cycle/.test(error):
35                 return CommonResourceServiceError.OWNERSHIP_CYCLE;
36             case /Mounts cannot be modified in state 'Final'/.test(error):
37                 return CommonResourceServiceError.MODIFYING_CONTAINER_REQUEST_FINAL_STATE;
38             case /Name has already been taken/.test(error):
39                 return CommonResourceServiceError.NAME_HAS_ALREADY_BEEN_TAKEN;
40             default:
41                 return CommonResourceServiceError.UNKNOWN;
42         }
43     }
44     return CommonResourceServiceError.NONE;
45 };
46
47