move ssh errors from common resource to auth keys service
[arvados-workbench2.git] / src / services / authorized-keys-service / authorized-keys-service.ts
index b51704a0223ddd84e7f23ae2176973d0ca304ed6..c952f4255244b52526cdf8c8148ec6c7660e35f0 100644 (file)
@@ -4,11 +4,31 @@
 
 import { AxiosInstance } from "axios";
 import { SshKeyResource } from '~/models/ssh-key';
-import { CommonResourceService } from "~/services/common-service/common-resource-service";
+import { CommonResourceService, CommonResourceServiceError } from '~/services/common-service/common-resource-service';
 import { ApiActions } from "~/services/api/api-actions";
 
+export enum AuthorizedKeysServiceError {
+    UNIQUE_PUBLIC_KEY = 'UniquePublicKey',
+    INVALID_PUBLIC_KEY = 'InvalidPublicKey',
+}
+
 export class AuthorizedKeysService extends CommonResourceService<SshKeyResource> {
     constructor(serverApi: AxiosInstance, actions: ApiActions) {
         super(serverApi, "authorized_keys", actions);
     }
-}
\ No newline at end of file
+}
+
+export const getAuthorizedKeysServiceError = (errorResponse: any) => {
+    if ('errors' in errorResponse && 'errorToken' in errorResponse) {
+        const error = errorResponse.errors.join('');
+        switch (true) {
+            case /Public key does not appear to be a valid ssh-rsa or dsa public key/.test(error):
+                return AuthorizedKeysServiceError.INVALID_PUBLIC_KEY;
+            case /Public key already exists in the database, use a different key./.test(error):
+                return AuthorizedKeysServiceError.UNIQUE_PUBLIC_KEY;
+            default:
+                return CommonResourceServiceError.UNKNOWN;
+        }
+    }
+    return CommonResourceServiceError.NONE;
+};
\ No newline at end of file