Skip to content
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

feat: Catch all errors from backend and save them into database #623

Merged
merged 3 commits into from
Dec 30, 2024
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
14 changes: 7 additions & 7 deletions apps/backend/src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { createGoogleGenerativeAI } from '@ai-sdk/google';
import { CacheModule } from '@nestjs/cache-manager';
import { Module } from '@nestjs/common';
// import { emailSMTP } from 'vitnode-backend-email-smtp';
import { emailResend } from 'vitnode-backend-email-resend';
// import { emailResend } from 'vitnode-backend-email-resend';
import { VitNodeCoreModule } from 'vitnode-backend/app.module';

import { DATABASE_ENVS, schemaDatabase } from './database/config';
Expand All @@ -23,13 +23,13 @@ const google = createGoogleGenerativeAI({
ai: google('gemini-2.0-flash-exp'),
captcha: {
type: 'cloudflare_turnstile',
secret_key: '0x4AAAAAAA32f41qx12_EeCCnJ4mQ6kV6w4',
site_key: '0x4AAAAAAA32f1b5Xe26DZHP',
secret_key: '',
site_key: '',
},
email: emailResend({
api_key: process.env.EMAIL_RESEND_API_KEY,
from: process.env.EMAIL_RESEND_FROM,
}),
// email: emailResend({
// api_key: process.env.EMAIL_RESEND_API_KEY,
// from: process.env.EMAIL_RESEND_FROM,
// }),
// email: emailSMTP({
// host: process.env.EMAIL_SMTP_HOST,
// port: process.env.EMAIL_SMTP_PORT,
Expand Down
7 changes: 4 additions & 3 deletions apps/backend/src/main.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { INestApplication } from '@nestjs/common';
import { INestApplication, NestApplicationOptions } from '@nestjs/common';
import { NestFactory } from '@nestjs/core';
import { nestjsMainApp } from 'vitnode-backend/main';
import { nestFactoryOptions, nestjsMainApp } from 'vitnode-backend/main';

import { AppModule } from './app.module';

async function bootstrap() {
const app: INestApplication = await NestFactory.create(AppModule);
const options: NestApplicationOptions = nestFactoryOptions;
const app: INestApplication = await NestFactory.create(AppModule, options);

void nestjsMainApp(app, {});
}
Expand Down
12 changes: 11 additions & 1 deletion apps/frontend/src/plugins/admin/langs/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,17 @@
},
"enable": "Enable",
"disable": "Disable",
"how_to_enable": "How to enable?"
"how_to_enable": "How to enable?",
"error_logs": {
"title": "Error Logs",
"desc": "If you have any issues with your website, you can check the error logs here.",
"name": "Name",
"message": "Message",
"created": "Created",
"more_info": "More Info",
"url": "URL",
"headers": "Headers"
}
},
"email": {
"hello": "Hello"
Expand Down
2 changes: 2 additions & 0 deletions packages/backend/src/core/admin/admin.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { AdvancedAdminModule } from './advanced/advanced.module';
import { AuthAdminModule } from './auth/auth.module';
import { DashboardAdminModule } from './dashboard/dashboard.module';
import { LanguagesAdminModule } from './languages/languages.module';
import { LogsAdminModule } from './logs/logs.module';
import { MembersAdminModule } from './members/members.module';
import { PluginsAdminModule } from './plugins/plugins.module';
import { SettingsAdminModule } from './settings/settings.module';
Expand All @@ -19,6 +20,7 @@ import { StylesAdminModule } from './styles/styles.module';
StylesAdminModule,
AdvancedAdminModule,
DashboardAdminModule,
LogsAdminModule,
],
})
export class AdminModule {}
24 changes: 24 additions & 0 deletions packages/backend/src/core/admin/logs/logs.controller.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { Controllers } from '@/helpers/controller.decorator';
import { Get, Query } from '@nestjs/common';
import { ApiOkResponse } from '@nestjs/swagger';
import {
ShowLogsAdminObj,
ShowLogsAdminQuery,
} from 'vitnode-shared/admin/logs.dto';

import { ShowLogsAdminService } from './service/show.service';

@Controllers({
plugin_name: 'Core',
plugin_code: 'logs',
isAdmin: true,
})
export class LogsAdminController {
constructor(private readonly showLogsAdminService: ShowLogsAdminService) {}

@ApiOkResponse({ type: ShowLogsAdminObj, description: 'Show logs' })
@Get()
async show(@Query() query: ShowLogsAdminQuery): Promise<ShowLogsAdminObj> {
return await this.showLogsAdminService.show(query);
}
}
10 changes: 10 additions & 0 deletions packages/backend/src/core/admin/logs/logs.module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { Module } from '@nestjs/common';

import { LogsAdminController } from './logs.controller';
import { ShowLogsAdminService } from './service/show.service';

@Module({
providers: [ShowLogsAdminService],
controllers: [LogsAdminController],
})
export class LogsAdminModule {}
40 changes: 40 additions & 0 deletions packages/backend/src/core/admin/logs/service/show.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { core_logs } from '@/database/schema/logs';
import { InternalDatabaseService } from '@/utils/database/internal_database.service';
import { Injectable } from '@nestjs/common';
import {
ShowLogsAdminObj,
ShowLogsAdminQuery,
} from 'vitnode-shared/admin/logs.dto';
import { SortDirectionEnum } from 'vitnode-shared/utils/pagination.enum';

@Injectable()
export class ShowLogsAdminService {
constructor(private readonly databaseService: InternalDatabaseService) {}

async show({
cursor,
first,
last,
}: ShowLogsAdminQuery): Promise<ShowLogsAdminObj> {
const pagination = await this.databaseService.paginationCursor({
cursor,
first,
last,
database: core_logs,
defaultSortBy: {
direction: SortDirectionEnum.desc,
column: 'created_at',
},
query: async args =>
await this.databaseService.db.query.core_logs.findMany(args),
});

return {
...pagination,
edges: pagination.edges.map(edge => ({
...edge,
headers: JSON.stringify(edge.headers),
})),
};
}
}
11 changes: 11 additions & 0 deletions packages/backend/src/database/schema/logs.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,16 @@
import { pgTable } from 'drizzle-orm/pg-core';

export const core_logs = pgTable('core_logs', t => ({
id: t.serial().primaryKey(),
name: t.varchar({ length: 255 }).notNull(),
message: t.text().notNull(),
status: t.smallint().notNull(),
created_at: t.timestamp().notNull().defaultNow(),
headers: t.jsonb().notNull(),
method: t.varchar({ length: 10 }).notNull(),
url: t.varchar({ length: 255 }).notNull(),
}));

export const core_logs_email = pgTable('core_logs_email', t => ({
id: t.serial().primaryKey(),
to: t.varchar({ length: 255 }).notNull(),
Expand Down
6 changes: 6 additions & 0 deletions packages/backend/src/helpers/helpers.module.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import { InternalAuthService } from '@/helpers/auth/internal_auth.service';
import { AllExceptionsFilter } from '@/utils/all-exceptions.filter';
import {
DynamicModule,
Global,
HttpException,
HttpStatus,
Module,
} from '@nestjs/common';
import { APP_FILTER } from '@nestjs/core';
import { LanguageModel } from 'ai';

import { AiHelperService } from './ai.service';
Expand Down Expand Up @@ -86,6 +88,10 @@ export class GlobalHelpersModule {
SSOAuthHelper,
ConfigHelperService,
AiHelperService,
{
provide: APP_FILTER,
useClass: AllExceptionsFilter,
},
],
exports: [
EmailHelperService,
Expand Down
11 changes: 10 additions & 1 deletion packages/backend/src/main.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
/* eslint-disable no-console */
import { INestApplication, ValidationPipe } from '@nestjs/common';
import {
INestApplication,
NestApplicationOptions,
ValidationPipe,
} from '@nestjs/common';
import { CorsOptions } from '@nestjs/common/interfaces/external/cors-options.interface';
import { DocumentBuilder, SwaggerModule } from '@nestjs/swagger';
import cookieParser from 'cookie-parser';
Expand Down Expand Up @@ -77,3 +81,8 @@ export const nestjsMainApp = async (app: INestApplication, options?: Args) => {
);
});
};

export const nestFactoryOptions: NestApplicationOptions = {
rawBody: true,
bodyParser: true,
};
57 changes: 57 additions & 0 deletions packages/backend/src/utils/all-exceptions.filter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import { core_logs } from '@/database/schema/logs';
import {
ArgumentsHost,
Catch,
HttpException,
Injectable,
} from '@nestjs/common';
import { BaseExceptionFilter } from '@nestjs/core';

import { InternalDatabaseService } from './database/internal_database.service';

@Catch()
@Injectable()
export class AllExceptionsFilter extends BaseExceptionFilter {
constructor(private readonly databaseService: InternalDatabaseService) {
super();
}

async catch(exception: unknown, host: ArgumentsHost) {
const ctx = host.switchToHttp();
const request = ctx.getRequest<Request>();

if (!(exception instanceof HttpException)) {
const unCatchError = exception as Error;

await this.databaseService.db.insert(core_logs).values({
name: 'Internal Server Error',
message: unCatchError.message,
status: 500,
headers: JSON.parse(JSON.stringify(request.headers)),
method: request.method,
url: request.url,
});

super.catch(exception, host);

return;
}

if (!exception.message.includes('InternalServerErrorException')) {
super.catch(exception, host);

return;
}

await this.databaseService.db.insert(core_logs).values({
name: 'InternalServerErrorException',
message: exception.message,
status: exception.getStatus(),
headers: JSON.parse(JSON.stringify(request.headers)),
method: request.method,
url: request.url,
});

super.catch(exception, host);
}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
import { INestApplication } from '@nestjs/common';
import { NestFactory } from '@nestjs/core';
import { nestjsMainApp } from 'vitnode-backend/main';
import { nestFactoryOptions, nestjsMainApp } from 'vitnode-backend/main';

import { AppModule } from './app.module';

async function bootstrap() {
const app: INestApplication = await NestFactory.create(AppModule);
const app: INestApplication = await NestFactory.create(
AppModule,
nestFactoryOptions,
);

void nestjsMainApp(app, {});
}
Expand Down
2 changes: 1 addition & 1 deletion packages/frontend/src/components/ui/alert-dialog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ const AlertDialogHeader = ({
}: React.HTMLAttributes<HTMLDivElement>) => (
<div
className={cn(
'flex flex-col space-y-2 text-center sm:text-left',
'flex flex-col space-y-1 text-center sm:text-left',
className,
)}
{...props}
Expand Down
2 changes: 1 addition & 1 deletion packages/frontend/src/components/ui/sheet.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ const SheetHeader = ({
}: React.HTMLAttributes<HTMLDivElement>) => (
<div
className={cn(
'flex flex-col space-y-2 text-center sm:text-left',
'flex flex-col space-y-1 text-center sm:text-left',
className,
)}
{...props}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { getMiddlewareData } from '@/api/get-middleware-data';
import { checkAdminPermissionPage } from '@/api/get-session-admin-data';
import { SkeletonDataTable } from '@/components/data-table/skeleton';
import { TranslationsProvider } from '@/components/translations-provider';
import { Badge } from '@/components/ui/badge';
import { Button } from '@/components/ui/button';
Expand All @@ -15,9 +16,11 @@ import {
} from 'lucide-react';
import { Metadata } from 'next';
import { getTranslations } from 'next-intl/server';
import React from 'react';

import { WarnReqRestartServer } from '../warn-req-restart-server';
import { ActionsDiagnosticTools } from './actions/actions';
import { LogsDiagnosticToolsView } from './logs/logs-diagnostic-tools-view';

const permission = {
plugin_code: 'core',
Expand All @@ -33,7 +36,11 @@ export const generateMetadataDiagnosticAdmin = async (): Promise<Metadata> => {
};
};

export const DiagnosticToolsView = async () => {
export const DiagnosticToolsView = async ({
searchParams,
}: {
searchParams: Promise<Record<string, string>>;
}) => {
const perm = await checkAdminPermissionPage(permission);
if (perm) return perm;
const t = await getTranslations('admin.core.diagnostic');
Expand Down Expand Up @@ -68,7 +75,7 @@ export const DiagnosticToolsView = async () => {

<WarnReqRestartServer />

<div className="grid auto-rows-min gap-4 md:grid-cols-3">
<div className="mb-10 grid auto-rows-min gap-4 md:grid-cols-3">
{quickLook.map(item => (
<Card className="p-6" key={item.id}>
<div className="flex items-start gap-4">
Expand Down Expand Up @@ -110,6 +117,12 @@ export const DiagnosticToolsView = async () => {
</Card>
))}
</div>

<HeaderContent desc={t('error_logs.desc')} h2={t('error_logs.title')} />

<React.Suspense fallback={<SkeletonDataTable />}>
<LogsDiagnosticToolsView searchParams={searchParams} />
</React.Suspense>
</TranslationsProvider>
);
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { LogsAdminObj } from 'vitnode-shared/admin/logs.dto';

import { MoreActionsLogsDiagnosticTools } from './more';

export const ActionsLogsDiagnosticTools = (props: LogsAdminObj) => {
return (
<>
<MoreActionsLogsDiagnosticTools {...props} />
</>
);
};
Loading
Loading