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

Fix error reporting #114

Closed
Closed
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
16 changes: 6 additions & 10 deletions lib/render.filter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,18 +78,14 @@ export class RenderFilter implements ExceptionFilter {
response.sent = true;
}

if (res.statusCode === HttpStatus.NOT_FOUND) {
if (this.service.passthroughNotFoundErrors()) {
return requestHandler(req, res);
}

return errorRenderer(null, req, res, pathname, {
...query,
[Symbol.for('Error')]: serializedErr,
});
if (
res.statusCode === HttpStatus.NOT_FOUND &&
this.service.passthroughNotFoundErrors()
) {
return requestHandler(req, res);
}

return errorRenderer(serializedErr, req, res, pathname, query);
return errorRenderer(host, serializedErr, req, res, pathname, query);
}

return;
Expand Down
49 changes: 43 additions & 6 deletions lib/render.module.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
import { DynamicModule, Module } from '@nestjs/common';
import { ApplicationConfig, HttpAdapterHost } from '@nestjs/core';
import {
ArgumentsHost,
DynamicModule,
HttpServer,
Module,
} from '@nestjs/common';
import {
ApplicationConfig,
BaseExceptionFilter,
HttpAdapterHost,
NestContainer,
} from '@nestjs/core';

import Server from 'next';
import type { DynamicRoutes } from 'next/dist/server/router';
Expand Down Expand Up @@ -28,13 +38,14 @@ export class RenderModule {
}

const nextConfig = (next as any).nextConfig;
const nextServer = (next as any).server;
const nextServer =
(next as any).server ?? (next as any).renderServer.server;

const basePath = nextConfig
? nextConfig.basePath
: nextServer.nextConfig.basePath;

const dynamicRoutes = (nextServer.dynamicRoutes as DynamicRoutes).map(
const dynamicRoutes = (nextServer.dynamicRoutes as DynamicRoutes)?.map(
(route) => route.page,
);

Expand All @@ -56,7 +67,20 @@ export class RenderModule {
config,
next.getRequestHandler(),
next.render.bind(next),
next.renderError.bind(next),
//next.renderError.bind(next),
async (
host: ArgumentsHost,
err: any,
req: any,
res: any,
pathname: any,
query?: any,
) => {
return new BaseExceptionFilter(nestHost.httpAdapter).catch(
err,
host,
);
},
nestHost.httpAdapter,
);
},
Expand Down Expand Up @@ -108,7 +132,20 @@ export class RenderModule {
this.service.mergeConfig(options);
this.service.setRequestHandler(next.getRequestHandler());
this.service.setRenderer(next.render.bind(next));
this.service.setErrorRenderer(next.renderError.bind(next));
this.service.setErrorRenderer(
async (
host: ArgumentsHost,
err: any,
req: any,
res: any,
pathname: any,
query?: any,
) => {
return new BaseExceptionFilter(
this.httpAdapterHost.httpAdapter,
).catch(err, host);
},
);
this.service.bindHttpServer(this.httpAdapterHost.httpAdapter);
this.applicationConfig.useGlobalFilters(new RenderFilter(this.service));
}
Expand Down
2 changes: 2 additions & 0 deletions lib/types.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { ArgumentsHost } from '@nestjs/common';
import { ParsedUrlQuery } from 'querystring';

export type RequestHandler = (req: any, res: any, query?: any) => Promise<void>;
Expand All @@ -14,6 +15,7 @@ export interface RenderableResponse<DataType extends unknown = ParsedUrlQuery> {
}

export type ErrorRenderer = (
host: ArgumentsHost,
err: any,
req: any,
res: any,
Expand Down
Loading