Skip to content

fix: rest api should return error reason #507

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions packages/server/src/api/rest/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1505,7 +1505,7 @@ class RequestHandler {
private handlePrismaError(err: unknown) {
if (isPrismaClientKnownRequestError(err)) {
if (err.code === 'P2004') {
return this.makeError('forbidden');
return this.makeError('forbidden', undefined, 403, err.meta?.reason as string);
} else if (err.code === 'P2025' || err.code === 'P2018') {
return this.makeError('notFound');
} else {
Expand All @@ -1530,7 +1530,7 @@ class RequestHandler {
}
}

private makeError(code: keyof typeof this.errors, detail?: string, status?: number) {
private makeError(code: keyof typeof this.errors, detail?: string, status?: number, reason?: string) {
return {
status: status ?? this.errors[code].status,
body: {
Expand All @@ -1540,6 +1540,7 @@ class RequestHandler {
code: paramCase(code),
title: this.errors[code].title,
detail: detail || this.errors[code].detail,
reason,
},
],
},
Expand Down
28 changes: 25 additions & 3 deletions packages/server/tests/api/rest.test.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
/* eslint-disable @typescript-eslint/no-explicit-any */
/// <reference types="@types/jest" />

import { loadSchema, run } from '@zenstackhq/testtools';
import { withPolicy } from '@zenstackhq/runtime';
import { CrudFailureReason } from '@zenstackhq/runtime/constants';
import { ModelMeta } from '@zenstackhq/runtime/enhancements/types';
import { loadSchema, run } from '@zenstackhq/testtools';
import makeHandler from '../../src/api/rest';
import { Response } from '../../src/types';
import { withPolicy } from '@zenstackhq/runtime';

let prisma: any;
let zodSchemas: any;
Expand Down Expand Up @@ -1844,6 +1845,13 @@ describe('REST server tests - enhanced prisma', () => {
@@allow('create,read', true)
@@allow('update', value > 0)
}

model Bar {
id Int @id
value Int

@@allow('create', true)
}
`;

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

it('policy rejection test', async () => {
it('update policy rejection test', async () => {
let r = await handler({
method: 'post',
path: '/foo',
Expand All @@ -1885,6 +1893,20 @@ describe('REST server tests - enhanced prisma', () => {
});
expect(r.status).toBe(403);
});

it('read-back policy rejection test', async () => {
const r = await handler({
method: 'post',
path: '/bar',
query: {},
requestBody: {
data: { type: 'bar', attributes: { id: 1, value: 0 } },
},
prisma,
});
expect(r.status).toBe(403);
expect((r.body as any).errors[0].reason).toBe(CrudFailureReason.RESULT_NOT_READABLE);
});
});

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