Skip to content

Commit 5b12687

Browse files
committed
better error messages
1 parent de95c5a commit 5b12687

File tree

1 file changed

+8
-3
lines changed

1 file changed

+8
-3
lines changed

src/utils.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,12 @@ const utils = {
1717
toStringArray(value:any):string[] {
1818
if (Array.isArray(value)) return value;
1919
if (typeof value === 'string') return value.trim().split(/\s*[;,]\s*/);
20-
throw new Error('Cannot convert value to array!');
20+
// throw new Error('Cannot convert value to array!');
21+
return null;
2122
},
2223

2324
isFilledStringArray(arr:any[]):boolean {
25+
if (!arr || !Array.isArray(arr)) return false;
2426
for (let s of arr) {
2527
if (typeof s !== 'string' || s.trim() === '') return false;
2628
}
@@ -56,6 +58,7 @@ const utils = {
5658
*/
5759
getFlatRoles(grants:any, roles:string|string[]):string[] {
5860
roles = utils.toStringArray(roles);
61+
if (!roles) throw new AccessControlError(`Invalid role(s): ${JSON.stringify(roles)}`);
5962
let arr:string[] = roles.concat();
6063
roles.forEach((roleName:string) => {
6164
let role:any = grants[roleName];
@@ -302,12 +305,14 @@ const utils = {
302305
*/
303306
extendRole(grants:any, roles:string|string[], extenderRoles:string|string[]) {
304307
let arrExtRoles:string[] = utils.toStringArray(extenderRoles);
308+
if (!arrExtRoles) throw new AccessControlError(`Invalid extender role(s): ${JSON.stringify(extenderRoles)}`);
305309
let nonExistentExtRoles:string[] = utils.getNonExistentRoles(grants, arrExtRoles);
306310
if (nonExistentExtRoles.length > 0) {
307311
throw new AccessControlError(`Cannot extend with non-existent role(s): "${nonExistentExtRoles.join(', ')}"`);
308312
}
309-
310-
utils.toStringArray(roles).forEach((role:string) => {
313+
roles = utils.toStringArray(roles);
314+
if (!roles) throw new AccessControlError(`Invalid role(s): ${JSON.stringify(roles)}`);
315+
roles.forEach((role:string) => {
311316
if (arrExtRoles.indexOf(role) >= 0) {
312317
throw new AccessControlError(`Attempted to extend role "${role}" by itself.`);
313318
}

0 commit comments

Comments
 (0)