Skip to content

Commit 3276faa

Browse files
committed
fix: permission request 500 errors
As far as I remember, PermissionService was not using APIError because it was considered to be only for errors from API endpoints and not internal calls. However, this never made very much sense: APIError is really more like "error that Puter's backend actually knows how to handle" at this point; everything should be APIError and APIError should probably eventually be renamed.
1 parent 1e86039 commit 3276faa

File tree

1 file changed

+26
-4
lines changed

1 file changed

+26
-4
lines changed

src/backend/src/services/auth/PermissionService.js

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
* along with this program. If not, see <https://www.gnu.org/licenses/>.
1919
*/
2020

21+
const APIError = require("../../api/APIError");
2122
const { get_user, get_app } = require("../../helpers");
2223
const BaseService = require("../BaseService");
2324
const { DB_WRITE } = require("../database/consts");
@@ -401,6 +402,12 @@ class PermissionService extends BaseService {
401402

402403
let app = await get_app({ uid: app_uid });
403404
if ( ! app ) app = await get_app({ name: app_uid });
405+
406+
if ( ! app ) {
407+
throw APIError.create('entity_not_found', null, {
408+
identifier: 'app:' + app_uid,
409+
});
410+
}
404411

405412
const app_id = app.id;
406413

@@ -466,6 +473,11 @@ class PermissionService extends BaseService {
466473

467474
let app = await get_app({ uid: app_uid });
468475
if ( ! app ) app = await get_app({ name: app_uid });
476+
if ( ! app ) {
477+
throw APIError.create('entity_not_found', null, {
478+
identifier: 'app' + app_uid,
479+
})
480+
}
469481
const app_id = app.id;
470482

471483
// DELETE permission
@@ -570,7 +582,9 @@ class PermissionService extends BaseService {
570582
permission = await this._rewrite_permission(permission);
571583
const user = await get_user({ username });
572584
if ( ! user ) {
573-
throw new Error('user not found');
585+
throw APIError.create('user_does_not_exist', null, {
586+
username,
587+
})
574588
}
575589

576590
// Don't allow granting permissions to yourself
@@ -633,7 +647,9 @@ class PermissionService extends BaseService {
633647
const svc_group = this.services.get('group');
634648
const group = await svc_group.get({ uid: gid });
635649
if ( ! group ) {
636-
throw new Error('group not found');
650+
throw APIError.create('entity_not_found', null, {
651+
identifier: 'group:' + gid,
652+
});
637653
}
638654

639655
await this.db.write(
@@ -688,7 +704,11 @@ class PermissionService extends BaseService {
688704

689705
const user = await get_user({ username });
690706
if ( ! user ) {
691-
throw new Error('user not found');
707+
if ( ! user ) {
708+
throw APIError.create('user_does_not_exist', null, {
709+
username,
710+
})
711+
}
692712
}
693713

694714
// DELETE permission
@@ -738,7 +758,9 @@ class PermissionService extends BaseService {
738758
const svc_group = this.services.get('group');
739759
const group = await svc_group.get({ uid: gid });
740760
if ( ! group ) {
741-
throw new Error('group not found');
761+
throw APIError.create('entity_not_found', null, {
762+
identifier: 'group:' + gid,
763+
});
742764
}
743765

744766
// DELETE permission

0 commit comments

Comments
 (0)