Skip to content

cors headers dropped on HTTPError (v2) #1301

Description

@carbohydrate

Environment

node - 22.19.0
h3 - 2.0.1-rc.14

Reproduction

import { H3, handleCors, HTTPError, serve } from 'h3';

const app = new H3();

app.post('/session', async (event) => {
    const corsRes = handleCors(event, {
        origin: ['http://localhost:5173'],
    });

    if (corsRes !== false) {
        return corsRes;
    }

    throw new HTTPError('Invalid Password!');
});

serve(app, {
    port: 3000,
});

Describe the bug

When a HTTPError is thrown cors headers are removed, which makes browser clients lose a bunch of relevant information about the error.

According to the docs this is how errors are supposed to be handled and doing throw new Error('message') will result in an unhandled error.

Additional context

No response

Logs

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions