feat: add initial api logging (#1494)

Improve API logging and error handling between client and server side.
This commit is contained in:
David Nguyen
2024-11-28 16:05:37 +07:00
committed by GitHub
parent 04293968c6
commit 98d85b086d
53 changed files with 933 additions and 780 deletions

View File

@ -101,7 +101,7 @@ export const createDocumentFromDirectTemplate = async ({
});
if (!template?.directLink?.enabled) {
throw new AppError(AppErrorCode.INVALID_REQUEST, 'Invalid or missing template');
throw new AppError(AppErrorCode.INVALID_REQUEST, { message: 'Invalid or missing template' });
}
const { Recipient: recipients, directLink, User: templateOwner } = template;
@ -111,15 +111,19 @@ export const createDocumentFromDirectTemplate = async ({
);
if (!directTemplateRecipient || directTemplateRecipient.role === RecipientRole.CC) {
throw new AppError(AppErrorCode.INVALID_REQUEST, 'Invalid or missing direct recipient');
throw new AppError(AppErrorCode.INVALID_REQUEST, {
message: 'Invalid or missing direct recipient',
});
}
if (template.updatedAt.getTime() !== templateUpdatedAt.getTime()) {
throw new AppError(AppErrorCode.INVALID_REQUEST, 'Template no longer matches');
throw new AppError(AppErrorCode.INVALID_REQUEST, { message: 'Template no longer matches' });
}
if (user && user.email !== directRecipientEmail) {
throw new AppError(AppErrorCode.INVALID_REQUEST, 'Email must match if you are logged in');
throw new AppError(AppErrorCode.INVALID_REQUEST, {
message: 'Email must match if you are logged in',
});
}
const { derivedRecipientAccessAuth, documentAuthOption: templateAuthOptions } =
@ -136,7 +140,7 @@ export const createDocumentFromDirectTemplate = async ({
.exhaustive();
if (!isAccessAuthValid) {
throw new AppError(AppErrorCode.UNAUTHORIZED, 'You must be logged in');
throw new AppError(AppErrorCode.UNAUTHORIZED, { message: 'You must be logged in' });
}
const directTemplateRecipientAuthOptions = ZRecipientAuthOptionsSchema.parse(
@ -163,7 +167,9 @@ export const createDocumentFromDirectTemplate = async ({
);
if (!signedFieldValue) {
throw new AppError(AppErrorCode.INVALID_BODY, 'Invalid, missing or changed fields');
throw new AppError(AppErrorCode.INVALID_BODY, {
message: 'Invalid, missing or changed fields',
});
}
if (templateField.type === FieldType.NAME && directRecipientName === undefined) {

View File

@ -120,7 +120,9 @@ export const createDocumentFromTemplate = async ({
});
if (!template) {
throw new AppError(AppErrorCode.NOT_FOUND, 'Template not found');
throw new AppError(AppErrorCode.NOT_FOUND, {
message: 'Template not found',
});
}
// Check that all the passed in recipient IDs can be associated with a template recipient.
@ -130,10 +132,9 @@ export const createDocumentFromTemplate = async ({
);
if (!foundRecipient) {
throw new AppError(
AppErrorCode.INVALID_BODY,
`Recipient with ID ${recipient.id} not found in the template.`,
);
throw new AppError(AppErrorCode.INVALID_BODY, {
message: `Recipient with ID ${recipient.id} not found in the template.`,
});
}
});

View File

@ -47,18 +47,18 @@ export const createTemplateDirectLink = async ({
});
if (!template) {
throw new AppError(AppErrorCode.NOT_FOUND, 'Template not found');
throw new AppError(AppErrorCode.NOT_FOUND, { message: 'Template not found' });
}
if (template.directLink) {
throw new AppError(AppErrorCode.ALREADY_EXISTS, 'Direct template already exists');
throw new AppError(AppErrorCode.ALREADY_EXISTS, { message: 'Direct template already exists' });
}
if (
directRecipientId &&
!template.Recipient.find((recipient) => recipient.id === directRecipientId)
) {
throw new AppError(AppErrorCode.NOT_FOUND, 'Recipient not found');
throw new AppError(AppErrorCode.NOT_FOUND, { message: 'Recipient not found' });
}
if (
@ -67,7 +67,9 @@ export const createTemplateDirectLink = async ({
(recipient) => recipient.email.toLowerCase() === DIRECT_TEMPLATE_RECIPIENT_EMAIL,
)
) {
throw new AppError(AppErrorCode.INVALID_BODY, 'Cannot generate placeholder direct recipient');
throw new AppError(AppErrorCode.INVALID_BODY, {
message: 'Cannot generate placeholder direct recipient',
});
}
return await prisma.$transaction(async (tx) => {

View File

@ -39,7 +39,9 @@ export const deleteTemplateDirectLink = async ({
});
if (!template) {
throw new AppError(AppErrorCode.NOT_FOUND, 'Template not found');
throw new AppError(AppErrorCode.NOT_FOUND, {
message: 'Template not found',
});
}
const { directLink } = template;

View File

@ -53,7 +53,9 @@ export const getTemplateById = async ({ id, userId, teamId }: GetTemplateByIdOpt
});
if (!template) {
throw new AppError(AppErrorCode.NOT_FOUND, 'Template not found');
throw new AppError(AppErrorCode.NOT_FOUND, {
message: 'Template not found',
});
}
return template;

View File

@ -1,6 +1,8 @@
import { prisma } from '@documenso/prisma';
import type { TemplateWithDetails } from '@documenso/prisma/types/template';
import { AppError, AppErrorCode } from '../../errors/app-error';
export type GetTemplateWithDetailsByIdOptions = {
id: number;
userId: number;
@ -10,7 +12,7 @@ export const getTemplateWithDetailsById = async ({
id,
userId,
}: GetTemplateWithDetailsByIdOptions): Promise<TemplateWithDetails> => {
return await prisma.template.findFirstOrThrow({
const template = await prisma.template.findFirst({
where: {
id,
OR: [
@ -36,4 +38,12 @@ export const getTemplateWithDetailsById = async ({
Field: true,
},
});
if (!template) {
throw new AppError(AppErrorCode.NOT_FOUND, {
message: 'Template not found',
});
}
return template;
};

View File

@ -40,13 +40,17 @@ export const toggleTemplateDirectLink = async ({
});
if (!template) {
throw new AppError(AppErrorCode.NOT_FOUND, 'Template not found');
throw new AppError(AppErrorCode.NOT_FOUND, {
message: 'Template not found',
});
}
const { directLink } = template;
if (!directLink) {
throw new AppError(AppErrorCode.NOT_FOUND, 'Direct template link not found');
throw new AppError(AppErrorCode.NOT_FOUND, {
message: 'Direct template link not found',
});
}
return await prisma.templateDirectLink.update({

View File

@ -34,7 +34,9 @@ export const updateTemplateSettings = async ({
data,
}: UpdateTemplateSettingsOptions) => {
if (Object.values(data).length === 0 && Object.keys(meta ?? {}).length === 0) {
throw new AppError(AppErrorCode.INVALID_BODY, 'Missing data to update');
throw new AppError(AppErrorCode.INVALID_BODY, {
message: 'Missing data to update',
});
}
const template = await prisma.template.findFirstOrThrow({
@ -82,10 +84,9 @@ export const updateTemplateSettings = async ({
});
if (!isDocumentEnterprise) {
throw new AppError(
AppErrorCode.UNAUTHORIZED,
'You do not have permission to set the action auth',
);
throw new AppError(AppErrorCode.UNAUTHORIZED, {
message: 'You do not have permission to set the action auth',
});
}
}