Add partial copy form async name validation
[arvados-workbench2.git] / src / common / api / common-resource-service.ts
index caa4d760c9e08ef1c7af63bf86e90b34a03d4ebb..da5bc33cf7c08f33af05e7e5c938c6b9e0663bc4 100644 (file)
@@ -29,6 +29,12 @@ export interface Errors {
     errorToken: string;
 }
 
+export enum CommonResourceServiceError {
+    UNIQUE_VIOLATION = 'UniqueViolation',
+    UNKNOWN = 'Unknown',
+    NONE = 'None'
+}
+
 export class CommonResourceService<T extends Resource> {
 
     static mapResponseKeys = (response: any): Promise<any> =>
@@ -106,3 +112,17 @@ export class CommonResourceService<T extends Resource> {
     }
 }
 
+export const getCommonResourceServiceError = (errorResponse: any) => {
+    if ('errors' in errorResponse && 'errorToken' in errorResponse) {
+        const error = errorResponse.errors.join('');
+        switch (true) {
+            case /UniqueViolation/.test(error):
+                return CommonResourceServiceError.UNIQUE_VIOLATION;
+            default:
+                return CommonResourceServiceError.UNKNOWN;
+        }
+    }
+    return CommonResourceServiceError.NONE;
+};
+
+