Skip to content

Commit

Permalink
refactor(backend): use process.exit instead of throw
Browse files Browse the repository at this point in the history
For some reason, listhen intercepts the unhadledRejection process, making global throws practically useless. See unjs/listhen#163

There is also a fix in the `onError signature` (which in reality had void returns)

Signed-off-by: Fernando Fernández <[email protected]>
  • Loading branch information
ferferga committed Dec 12, 2024
1 parent fac4ce1 commit 47aeb4b
Show file tree
Hide file tree
Showing 4 changed files with 7 additions and 5 deletions.
2 changes: 1 addition & 1 deletion packages/backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"start": "npm run prisma && NODE_OPTIONS='--experimental-strip-types' NODE_NO_WARNINGS=1 npm run listhen:dev",
"prod": "npm run prisma && NODE_OPTIONS='--experimental-strip-types' NODE_NO_WARNINGS=1 npm run listhen:prod",
"listhen:dev": "DEV=1 listhen -w ./src/app.ts",
"listhen:prod": "listhen --host --qr false ./src/app.ts",
"listhen:prod": "listhen --public --qr false ./src/app.ts",
"check": "npm run check:types",
"check:types": "tsc",
"lint": "eslint . --flag unstable_ts_config",
Expand Down
4 changes: 2 additions & 2 deletions packages/backend/src/app.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { join } from 'node:path';
import { mkdir } from 'node:fs/promises';
import { seedDb } from './util/db.ts';
import { createApp, createRouter, handleCors } from 'h3';
import { createApp, createRouter, handleCors, sendNoContent } from 'h3';
import { isDev } from './util/logger.ts';
import { registerDynamicModules } from './util/dynamic-modules.ts';
import { destr } from 'destr';
Expand Down Expand Up @@ -37,7 +37,7 @@ registerMailing();
*/
export const app = createApp({
debug: isDev,
onError: () => new Response(null, { status: 500 }),
onError: (_, event) => sendNoContent(event, 500),
onRequest: (event) => {
handleCors(event, {
origin: isDev || Boolean(process.env.ALLOW_ALL_CORS) ? '*' : destr(process.env.CORS_ORIGINS),
Expand Down
3 changes: 2 additions & 1 deletion packages/backend/src/redsys.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ if (!process.env.REDSYS_MERCHANT_CODE
if (isDev) {
console.warn(`${msg}.\nRedsys testing values will be used in development, but make sure to set those in production`);
} else {
throw new Error(`${msg}.\nHalting...`);
console.error(`${msg}.\nHalting...`);
process.exit(1);
}
}

Expand Down
3 changes: 2 additions & 1 deletion packages/backend/src/util/mailing/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ if (!is_available) {
if (isDev) {
console.warn(`${msg}.\nContinuing without mailing capabilities, but make sure to set those in production`);
} else {
throw new Error(`${msg}.\nPlease set those in the environment variables.\n\nHalting...`);
console.error(`${msg}.\nPlease set those in the environment variables.\n\nHalting...`);
process.exit(1);
}
}

Expand Down

0 comments on commit 47aeb4b

Please sign in to comment.