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: Add middleware to resend errors, fix regex #50

Open
wants to merge 1 commit into
base: development
Choose a base branch
from
Open
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: 14 additions & 0 deletions packages/database/supabase/functions/_shared/resendErrors.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { HttpError, Middleware } from 'oak';

export function resendErrors(): Middleware {
return async (ctx, next) => {
await next().catch((err) => {
if (err instanceof HttpError) {
ctx.response.status = err.status;
ctx.response.body = err.message;
} else {
throw err;
}
});
};
}
10 changes: 6 additions & 4 deletions packages/database/supabase/functions/app/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@ import authRouter from '../_routes/authRouter.ts';
import projectRouter from '../_routes/projectRouter.ts';
import reviewRouter from '../_routes/reviewRouter.ts';
import { logError } from '../_shared/logError.ts';
import { resendErrors } from '../_shared/resendErrors.ts';

const corsList = {
local: ['http://localhost:4200'],
development: [
new RegExp(/^https:\/\/[\w\d]+--chocolatenetwork\.netlify\.app$/, 'gm'),
],
development: [/(chocolatenetwork\.netlify\.app)$/],
production: ['https://chocolatenetwork.netlify.app'],
};
const env = Deno.env.get('APP_ENV') as keyof typeof corsList | undefined;
const origin = corsList[env || 'development'];

const router = new Router();
router
Expand All @@ -23,12 +23,14 @@ router

const app = new Application();
app.addEventListener('error', logError);
app.use(resendErrors());
app.use(
oakCors({
origin: corsList[env || 'local'],
origin: origin,
})
);
app.use(router.routes());
app.use(router.allowedMethods());

await app.listen({ port: 8000 });