FIX: exceeding Notes limit should be a 401 not 500
This commit is contained in:
6
lib/services/errors.ts
Normal file
6
lib/services/errors.ts
Normal file
@@ -0,0 +1,6 @@
|
|||||||
|
export class AccountLimitError extends Error {
|
||||||
|
constructor(message: string) {
|
||||||
|
super(message);
|
||||||
|
Object.setPrototypeOf(this, AccountLimitError.prototype);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,5 +1,6 @@
|
|||||||
import prisma_client from '~~/prisma/prisma.client';
|
import prisma_client from '~~/prisma/prisma.client';
|
||||||
import { openai } from './openai.client';
|
import { openai } from './openai.client';
|
||||||
|
import { AccountLimitError } from './errors';
|
||||||
|
|
||||||
export default class NotesService {
|
export default class NotesService {
|
||||||
async getAllNotes() {
|
async getAllNotes() {
|
||||||
@@ -21,7 +22,7 @@ export default class NotesService {
|
|||||||
});
|
});
|
||||||
|
|
||||||
if(account.notes.length>= account.max_notes){
|
if(account.notes.length>= account.max_notes){
|
||||||
throw new Error('Note Limit reached, no new notes can be added');
|
throw new AccountLimitError('Note Limit reached, no new notes can be added');
|
||||||
}
|
}
|
||||||
|
|
||||||
return prisma_client.note.create({ data: { account_id, note_text }});
|
return prisma_client.note.create({ data: { account_id, note_text }});
|
||||||
|
|||||||
@@ -11,9 +11,24 @@ import { initTRPC, TRPCError } from '@trpc/server'
|
|||||||
import { Context } from './context';
|
import { Context } from './context';
|
||||||
import { ACCOUNT_ACCESS } from '~~/prisma/account-access-enum';
|
import { ACCOUNT_ACCESS } from '~~/prisma/account-access-enum';
|
||||||
import superjson from 'superjson';
|
import superjson from 'superjson';
|
||||||
|
import { AccountLimitError } from '~~/lib/services/errors';
|
||||||
|
|
||||||
const t = initTRPC.context<Context>().create({
|
const t = initTRPC.context<Context>().create({
|
||||||
transformer: superjson,
|
transformer: superjson,
|
||||||
|
errorFormatter: (opts)=> {
|
||||||
|
const { shape, error } = opts;
|
||||||
|
if (!(error.cause instanceof AccountLimitError)) {
|
||||||
|
return shape;
|
||||||
|
}
|
||||||
|
return {
|
||||||
|
...shape,
|
||||||
|
data: {
|
||||||
|
...shape.data,
|
||||||
|
httpStatus: 401,
|
||||||
|
code: 'UNAUTHORIZED'
|
||||||
|
},
|
||||||
|
};
|
||||||
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
Reference in New Issue
Block a user