Skip to content

Commit 4b389fb

Browse files
authored
fix: rest api should return error reason (#507)
1 parent 5ced0fc commit 4b389fb

File tree

2 files changed

+28
-5
lines changed

2 files changed

+28
-5
lines changed

packages/server/src/api/rest/index.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1505,7 +1505,7 @@ class RequestHandler {
15051505
private handlePrismaError(err: unknown) {
15061506
if (isPrismaClientKnownRequestError(err)) {
15071507
if (err.code === 'P2004') {
1508-
return this.makeError('forbidden');
1508+
return this.makeError('forbidden', undefined, 403, err.meta?.reason as string);
15091509
} else if (err.code === 'P2025' || err.code === 'P2018') {
15101510
return this.makeError('notFound');
15111511
} else {
@@ -1530,7 +1530,7 @@ class RequestHandler {
15301530
}
15311531
}
15321532

1533-
private makeError(code: keyof typeof this.errors, detail?: string, status?: number) {
1533+
private makeError(code: keyof typeof this.errors, detail?: string, status?: number, reason?: string) {
15341534
return {
15351535
status: status ?? this.errors[code].status,
15361536
body: {
@@ -1540,6 +1540,7 @@ class RequestHandler {
15401540
code: paramCase(code),
15411541
title: this.errors[code].title,
15421542
detail: detail || this.errors[code].detail,
1543+
reason,
15431544
},
15441545
],
15451546
},

packages/server/tests/api/rest.test.ts

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
/* eslint-disable @typescript-eslint/no-explicit-any */
22
/// <reference types="@types/jest" />
33

4-
import { loadSchema, run } from '@zenstackhq/testtools';
4+
import { withPolicy } from '@zenstackhq/runtime';
5+
import { CrudFailureReason } from '@zenstackhq/runtime/constants';
56
import { ModelMeta } from '@zenstackhq/runtime/enhancements/types';
7+
import { loadSchema, run } from '@zenstackhq/testtools';
68
import makeHandler from '../../src/api/rest';
79
import { Response } from '../../src/types';
8-
import { withPolicy } from '@zenstackhq/runtime';
910

1011
let prisma: any;
1112
let zodSchemas: any;
@@ -1844,6 +1845,13 @@ describe('REST server tests - enhanced prisma', () => {
18441845
@@allow('create,read', true)
18451846
@@allow('update', value > 0)
18461847
}
1848+
1849+
model Bar {
1850+
id Int @id
1851+
value Int
1852+
1853+
@@allow('create', true)
1854+
}
18471855
`;
18481856

18491857
beforeAll(async () => {
@@ -1862,7 +1870,7 @@ describe('REST server tests - enhanced prisma', () => {
18621870
run('npx prisma db push');
18631871
});
18641872

1865-
it('policy rejection test', async () => {
1873+
it('update policy rejection test', async () => {
18661874
let r = await handler({
18671875
method: 'post',
18681876
path: '/foo',
@@ -1885,6 +1893,20 @@ describe('REST server tests - enhanced prisma', () => {
18851893
});
18861894
expect(r.status).toBe(403);
18871895
});
1896+
1897+
it('read-back policy rejection test', async () => {
1898+
const r = await handler({
1899+
method: 'post',
1900+
path: '/bar',
1901+
query: {},
1902+
requestBody: {
1903+
data: { type: 'bar', attributes: { id: 1, value: 0 } },
1904+
},
1905+
prisma,
1906+
});
1907+
expect(r.status).toBe(403);
1908+
expect((r.body as any).errors[0].reason).toBe(CrudFailureReason.RESULT_NOT_READABLE);
1909+
});
18881910
});
18891911

18901912
describe('REST server tests - NextAuth project regression', () => {

0 commit comments

Comments
 (0)