Skip to content

Feature Request: Bun.serve.error with Request parameter #29563

@SaltyAom

Description

@SaltyAom

What is the problem this feature would solve?

Current Bun.serve.error only provides an Error as a callback while fetch accepts request

Bun.serve({
    port: 3000,
    fetch(request) {
        throw new Error('thing')
    },
    error(error) {
        return new Response('thing')
    }
})

This makes error a "generic catch-all function" instead of being able to be tailored to a specific need for each request, similar to fetch. Leaving error unused most of the time

Bun.serve({
    port: 3000,
    fetch(request) {
        try {
            throw new Error('thing')
        } catch (error) {
            return handleErrorForEachRequest(request)
        }
    },
    error(error) {
        // Unable to tailor based on request, default to try-catch inside fetch
        return new Response('thing')
    }
})

What is the feature you are proposing to solve the problem?

Add request as a 2nd parameter to error callback

This wouldn't introduce any breaking change with any prior versions

Bun.serve({
    port: 3000,
    fetch(request) {
        throw new Error('thing')
    },
    // Request is added here
    error(error, request) {
        return handleErrorForEachRequest(request)
    }
})

What alternatives have you considered?

Keep using try-catch as provided

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions