17319: Throws exceptions when attempting to use "" as uuid on service calls.
[arvados.git] / src / services / common-service / common-service.ts
index 44233eb17bd2f683ecf4fcb48459160dd583119e..54c0edf6bf2c684346e0309103ad085c4f38a541 100644 (file)
@@ -9,6 +9,7 @@ import { ApiActions } from "~/services/api/api-actions";
 import * as QueryString from "query-string";
 
 interface Errors {
+    status: number;
     errors: string[];
     errorToken: string;
 }
@@ -21,6 +22,7 @@ export interface ListArguments {
     select?: string[];
     distinct?: boolean;
     count?: string;
+    includeOldVersions?: boolean;
 }
 
 export interface ListResults<T> {
@@ -66,7 +68,13 @@ export class CommonService<T> {
             }
         }
 
-    static defaultResponse<R>(promise: AxiosPromise<R>, actions: ApiActions, mapKeys = true): Promise<R> {
+    private validateUuid(uuid: string) {
+        if (uuid === "") {
+            throw new Error('UUID cannot be empty string');
+        }
+    }
+
+    static defaultResponse<R>(promise: AxiosPromise<R>, actions: ApiActions, mapKeys = true, showErrors = true): Promise<R> {
         const reqId = uuid();
         actions.progressFn(reqId, true);
         return promise
@@ -80,7 +88,8 @@ export class CommonService<T> {
             .catch(({ response }) => {
                 actions.progressFn(reqId, false);
                 const errors = CommonService.mapResponseKeys(response) as Errors;
-                actions.errorFn(reqId, errors);
+                errors.status = response.status;
+                actions.errorFn(reqId, errors, showErrors);
                 throw errors;
             });
     }
@@ -94,6 +103,7 @@ export class CommonService<T> {
     }
 
     delete(uuid: string): Promise<T> {
+        this.validateUuid(uuid);
         return CommonService.defaultResponse(
             this.serverApi
                 .delete(this.resourceType + '/' + uuid),
@@ -101,18 +111,21 @@ export class CommonService<T> {
         );
     }
 
-    get(uuid: string) {
+    get(uuid: string, showErrors?: boolean) {
+        this.validateUuid(uuid);
         return CommonService.defaultResponse(
             this.serverApi
                 .get<T>(this.resourceType + '/' + uuid),
-            this.actions
+            this.actions,
+            true, // mapKeys
+            showErrors
         );
     }
 
     list(args: ListArguments = {}): Promise<ListResults<T>> {
         const { filters, order, ...other } = args;
         const params = {
-            ...other,
+            ...CommonService.mapKeys(_.snakeCase)(other),
             filters: filters ? `[${filters}]` : undefined,
             order: order ? order : undefined
         };
@@ -143,6 +156,7 @@ export class CommonService<T> {
     }
 
     update(uuid: string, data: Partial<T>) {
+        this.validateUuid(uuid);
         return CommonService.defaultResponse(
             this.serverApi
                 .put<T>(this.resourceType + '/' + uuid, data && CommonService.mapKeys(_.snakeCase)(data)),