Skip to content

Commit d08ead3

Browse files
committed
fix(use): process global is not available in all environments and NODE_ENV doesn't necessarily depict production vs. development
1 parent 81b1a68 commit d08ead3

File tree

6 files changed

+5
-112
lines changed

6 files changed

+5
-112
lines changed

src/use/express.ts

+1-19
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@ export type HandlerOptions<Context extends OperationContext = undefined> =
4343
export function createHandler<Context extends OperationContext = undefined>(
4444
options: HandlerOptions<Context>,
4545
): Handler {
46-
const isProd = process.env.NODE_ENV === 'production';
4746
const handle = createRawHandler(options);
4847
return async function requestListener(req, res) {
4948
try {
@@ -74,24 +73,7 @@ export function createHandler<Context extends OperationContext = undefined>(
7473
'Please check your implementation.',
7574
err,
7675
);
77-
if (isProd) {
78-
res.writeHead(500).end();
79-
} else {
80-
res
81-
.writeHead(500, { 'content-type': 'application/json; charset=utf-8' })
82-
.end(
83-
JSON.stringify({
84-
errors: [
85-
err instanceof Error
86-
? {
87-
message: err.message,
88-
stack: err.stack,
89-
}
90-
: err,
91-
],
92-
}),
93-
);
94-
}
76+
res.writeHead(500).end();
9577
}
9678
};
9779
}

src/use/fastify.ts

+1-18
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@ export type HandlerOptions<Context extends OperationContext = undefined> =
4343
export function createHandler<Context extends OperationContext = undefined>(
4444
options: HandlerOptions<Context>,
4545
): RouteHandler {
46-
const isProd = process.env.NODE_ENV === 'production';
4746
const handle = createRawHandler(options);
4847
return async function requestListener(req, reply) {
4948
try {
@@ -69,23 +68,7 @@ export function createHandler<Context extends OperationContext = undefined>(
6968
'Please check your implementation.',
7069
err,
7170
);
72-
if (isProd) {
73-
reply.status(500).send();
74-
} else {
75-
reply
76-
.status(500)
77-
.header('content-type', 'application/json; charset=utf-8')
78-
.send({
79-
errors: [
80-
err instanceof Error
81-
? {
82-
message: err.message,
83-
stack: err.stack,
84-
}
85-
: err,
86-
],
87-
});
88-
}
71+
reply.status(500).send();
8972
}
9073
};
9174
}

src/use/fetch.ts

+1-23
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,6 @@ export function createHandler<Context extends OperationContext = undefined>(
5555
options: HandlerOptions<Context>,
5656
reqCtx: Partial<FetchAPI> = {},
5757
): (req: Request) => Promise<Response> {
58-
const isProd = process.env.NODE_ENV === 'production';
5958
const api: FetchAPI = {
6059
Response: reqCtx.Response || Response,
6160
TextEncoder: reqCtx.TextEncoder || TextEncoder,
@@ -81,28 +80,7 @@ export function createHandler<Context extends OperationContext = undefined>(
8180
'Please check your implementation.',
8281
err,
8382
);
84-
if (isProd) {
85-
return new api.Response(null, { status: 500 });
86-
} else {
87-
return new api.Response(
88-
JSON.stringify({
89-
errors: [
90-
err instanceof Error
91-
? {
92-
message: err.message,
93-
stack: err.stack,
94-
}
95-
: err,
96-
],
97-
}),
98-
{
99-
status: 500,
100-
headers: {
101-
'content-type': 'application/json; charset=utf-8',
102-
},
103-
},
104-
);
105-
}
83+
return new api.Response(null, { status: 500 });
10684
}
10785
};
10886
}

src/use/http.ts

+1-19
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@ export type HandlerOptions<Context extends OperationContext = undefined> =
4242
export function createHandler<Context extends OperationContext = undefined>(
4343
options: HandlerOptions<Context>,
4444
): (req: IncomingMessage, res: ServerResponse) => Promise<void> {
45-
const isProd = process.env.NODE_ENV === 'production';
4645
const handle = createRawHandler(options);
4746
return async function requestListener(req, res) {
4847
try {
@@ -74,24 +73,7 @@ export function createHandler<Context extends OperationContext = undefined>(
7473
'Please check your implementation.',
7574
err,
7675
);
77-
if (isProd) {
78-
res.writeHead(500).end();
79-
} else {
80-
res
81-
.writeHead(500, { 'content-type': 'application/json; charset=utf-8' })
82-
.end(
83-
JSON.stringify({
84-
errors: [
85-
err instanceof Error
86-
? {
87-
message: err.message,
88-
stack: err.stack,
89-
}
90-
: err,
91-
],
92-
}),
93-
);
94-
}
76+
res.writeHead(500).end();
9577
}
9678
};
9779
}

src/use/http2.ts

+1-19
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,6 @@ export type HandlerOptions<Context extends OperationContext = undefined> =
5454
export function createHandler<Context extends OperationContext = undefined>(
5555
options: HandlerOptions<Context>,
5656
): (req: Http2ServerRequest, res: Http2ServerResponse) => Promise<void> {
57-
const isProd = process.env.NODE_ENV === 'production';
5857
const handle = createRawHandler(options);
5958
return async function requestListener(req, res) {
6059
try {
@@ -91,24 +90,7 @@ export function createHandler<Context extends OperationContext = undefined>(
9190
'Please check your implementation.',
9291
err,
9392
);
94-
if (isProd) {
95-
res.writeHead(500).end();
96-
} else {
97-
res
98-
.writeHead(500, { 'content-type': 'application/json; charset=utf-8' })
99-
.end(
100-
JSON.stringify({
101-
errors: [
102-
err instanceof Error
103-
? {
104-
message: err.message,
105-
stack: err.stack,
106-
}
107-
: err,
108-
],
109-
}),
110-
);
111-
}
93+
res.writeHead(500).end();
11294
}
11395
};
11496
}

src/use/koa.ts

-14
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,6 @@ export type HandlerOptions<Context extends OperationContext = undefined> =
4545
export function createHandler<Context extends OperationContext = undefined>(
4646
options: HandlerOptions<Context>,
4747
): Middleware {
48-
const isProd = process.env.NODE_ENV === 'production';
4948
const handle = createRawHandler(options);
5049
return async function requestListener(ctx) {
5150
try {
@@ -84,19 +83,6 @@ export function createHandler<Context extends OperationContext = undefined>(
8483
err,
8584
);
8685
ctx.response.status = 500;
87-
if (!isProd) {
88-
ctx.response.set('content-type', 'application/json; charset=utf-8');
89-
ctx.body = {
90-
errors: [
91-
err instanceof Error
92-
? {
93-
message: err.message,
94-
stack: err.stack,
95-
}
96-
: err,
97-
],
98-
};
99-
}
10086
}
10187
};
10288
}

0 commit comments

Comments
 (0)