Browse Source

parseRole: improve error management

Olivier Massot 2 years ago
parent
commit
b89731f16b
1 changed files with 12 additions and 11 deletions
  1. 12 11
      services/rights/roleUtils.ts

+ 12 - 11
services/rights/roleUtils.ts

@@ -97,20 +97,21 @@ class RoleUtils {
   static parseRole(role: string): Role {
     const parts = role.split('_')
 
-    if (parts[0] !== 'ROLE') {
-      throw new Error('can not parse role')
-    }
-    parts.shift()
+    try {
+      parts.shift()
 
-    let action: 'VIEW' | 'CORE' | 'REFERENCE' | '' = ''
-    if (actions.includes(parts.at(-1) ?? '')) {
-      // @ts-ignore
-      action = parts.pop() ?? ''
-    }
+      let action: 'VIEW' | 'CORE' | 'REFERENCE' | '' = ''
+      if (actions.includes(parts.at(-1) ?? '')) {
+        // @ts-ignore
+        action = parts.pop() ?? ''
+      }
 
-    const subject = parts.join('-')
+      const subject = parts.join('-')
 
-    return { subject, action }
+      return { subject, action }
+    } catch (error) {
+      throw new Error('can not parse role: ' + role, { cause: error })
+    }
   }
 
   static roleToString(role: Role) {