Skip to content
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
2 changes: 1 addition & 1 deletion .github/workflows/backend.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ jobs:
build-test:
strategy:
matrix:
os: [windows-latest]
os: [ubuntu-latest]

runs-on: ${{matrix.os}}

Expand Down
15 changes: 14 additions & 1 deletion backend/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
// ChenAIKit Backend Server
import 'reflect-metadata';
import express, { Request, Response } from 'express';
import dotenv from 'dotenv';
dotenv.config();

// Initialize Sentry first
import { initSentry, sentryErrorHandler } from './middleware/errorTracking';
if (process.env.SENTRY_DSN) {
initSentry(process.env.SENTRY_DSN, process.env.NODE_ENV || 'development');
}

import express, { Request, Response } from 'express';
import { log } from './utils/logger';
import { requestLoggingMiddleware } from './middleware/logging';
import healthRouter from './routes/health';
Expand Down Expand Up @@ -79,6 +87,11 @@ app.use('*', (req: Request, res: Response) => {
});
});

// Sentry error handler (must be before other error handlers)
if (process.env.SENTRY_DSN) {
app.use(sentryErrorHandler());
}

// Global error handler
app.use((error: Error, req: express.Request, res: express.Response, _next: express.NextFunction) => {
console.error('Unhandled error:', error);
Expand Down
19 changes: 11 additions & 8 deletions backend/src/middleware/errorTracking.ts
Original file line number Diff line number Diff line change
@@ -1,28 +1,31 @@
import * as Sentry from '@sentry/node';
import { httpIntegration, expressIntegration, expressErrorHandler } from '@sentry/node';
import { Request, Response, NextFunction } from 'express';

export function initSentry(dsn: string, environment: string = 'production'): void {
const integrations: any[] = [
httpIntegration(),
expressIntegration(),
];
Sentry.init({
dsn,
environment,
tracesSampleRate: environment === 'production' ? 0.1 : 1.0,
integrations: [
new Sentry.Integrations.Http({ tracing: true }),
new Sentry.Integrations.Express({ app: undefined as any })
]
integrations
});
}

// For backward compatibility, these functions can return no-ops or be simplified
export function sentryRequestHandler() {
return Sentry.Handlers.requestHandler();
return (_req: Request, _res: Response, next: NextFunction) => next();
}

export function sentryTracingHandler() {
return Sentry.Handlers.tracingHandler();
return (_req: Request, _res: Response, next: NextFunction) => next();
}
Comment thread
coderabbitai[bot] marked this conversation as resolved.

export function sentryErrorHandler() {
return Sentry.Handlers.errorHandler();
export function sentryErrorHandler(): any {
return expressErrorHandler();
}

export function errorTrackingMiddleware(
Expand Down
22 changes: 7 additions & 15 deletions backend/src/server.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,14 @@
import express from 'express';
import {
initSentry,
sentryRequestHandler,
sentryTracingHandler,
sentryErrorHandler,
errorTrackingMiddleware
} from './middleware/errorTracking';
import healthRouter, { registerHealthCheck } from './routes/health';

const app = express();

// Initialize Sentry
// Initialize Sentry first before creating Express app
import { initSentry, sentryErrorHandler, errorTrackingMiddleware } from './middleware/errorTracking';
if (process.env.SENTRY_DSN) {
initSentry(process.env.SENTRY_DSN, process.env.NODE_ENV || 'development');
app.use(sentryRequestHandler());
app.use(sentryTracingHandler());
}

import express, { Application } from 'express';
import healthRouter, { registerHealthCheck } from './routes/health';

const app: Application = express();

app.use(express.json());

// Health checks
Expand Down
17 changes: 9 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,22 +46,23 @@
"author": "ChenAIKit Team",
"license": "MIT",
"devDependencies": {
"@testing-library/jest-dom": "^5.16.0",
"@testing-library/react": "^13.4.0",
"@testing-library/react-hooks": "^8.0.1",
"@types/jest": "^29.0.0",
"@types/node": "^20.0.0",
"@typescript-eslint/eslint-plugin": "^6.21.0",
"@typescript-eslint/parser": "^6.21.0",
"eslint": "^8.0.0",
"husky": "^8.0.3",
"identity-obj-proxy": "^3.0.0",
"jest": "^29.0.0",
"jest-environment-jsdom": "^29.7.0",
"lint-staged": "^15.0.0",
"prettier": "^3.0.0",
"typescript": "^5.0.0",
"ts-jest": "^29.1.0",
"supertest": "^7.2.2",
"@testing-library/jest-dom": "^5.16.0",
"@testing-library/react": "^13.4.0",
"@testing-library/react-hooks": "^8.0.1",
"husky": "^8.0.3",
"lint-staged": "^15.0.0",
"identity-obj-proxy": "^3.0.0"
"ts-jest": "^29.1.0",
"typescript": "^5.0.0"
},
"engines": {
"node": ">=18.0.0",
Expand Down
Loading
Loading