cli icon indicating copy to clipboard operation
cli copied to clipboard

Netlify dev tries to resolve to a page when returning 404 from a function

Open ercgrat opened this issue 3 years ago • 6 comments

Describe the bug

I have a FaunaDB CRUD API that I updated to pass on the exact HTTP status code from the database request so that I could catch 404 errors on the client. When my function returns 404, something (not my client code, probably netlify middleware - I checked the Network log in Chrome) initiates another function request that tacks on '.html' to my API route. Snippet of the output:

Request from ::1: GET /.netlify/functions/cart/279578027748229641
.
.
.
Response with status 404 in 204 ms. // This is the 404 that is correctly returned
Request from ::1: GET /.netlify/functions/cart/279578027748229641.html // A request to the same route plus '.html' follows immediately

This causes errors in code which is expecting numeric input for the id. It also forces me to return a non-404 error.

To Reproduce

Steps to reproduce the behavior:

  1. Create a function that returns a 404 error
  2. Execute the function in netlify dev
  3. Observe the above behavior

Configuration

Here's my actual function code:

/* Import faunaDB sdk */
const faunadb = require('faunadb');

const q = faunadb.query;
const client = new faunadb.Client({
    secret: process.env.FAUNA_DB_SECRET,
    timeout: 30
});

exports.handler = async event => {
    const id = event.id;
    console.log(`Function 'read' invoked. Read id: ${id}`)
    return client
        .query(q.Get(q.Ref(q.Collection('carts'), id)))
        .then(response => {
            console.log('success', response)
            return {
                statusCode: 200,
                body: JSON.stringify(response),
            }
        })
        .catch(error => {
            console.log('error', error)
            return {
                statusCode: error.requestResult.statusCode,
                body: JSON.stringify(error),
            }
        });
}

Expected behavior

The 404 error gets returned to the client.

CLI Output

See problem description.

ercgrat avatar Oct 16 '20 22:10 ercgrat

Reported also in https://github.com/netlify/cli/issues/695#issuecomment-725626061

erezrokah avatar Nov 12 '20 11:11 erezrokah

Still affected by this bug via builders!

Seems like returning anything other than HTTP status code 200 from my builder results in receiving a JSON request body with the text: Function is not an on-demand builder. See https://ntl.fyi/create-builder for how to convert a function to a builder.

But when I deploy, it works fine online!

whistlegraph avatar Jul 20 '22 05:07 whistlegraph

@digitpain Do you have any public repository that can be shared so I can reproduce the issue?

tinfoil-knight avatar Aug 06 '22 11:08 tinfoil-knight

If it helps this is the repo I was referencing in the original issue: https://github.com/ercgrat/creator.urbanbasic.de

ercgrat avatar Aug 06 '22 15:08 ercgrat

@ercgrat Thank you for providing the repo.

I ran the repo locally with netlify-cli/10.15.0 & there aren't any additional requests to the same route with .html anymore.

image

Note: I edited the code here a bit to always return the error status as 404.

.catch((error) => {
    console.log('error', error);
    return {
        statusCode: 404,
        body: JSON.stringify(error),
    };
});

tinfoil-knight avatar Aug 07 '22 10:08 tinfoil-knight

@danez I think this issue can be closed.

tinfoil-knight avatar Aug 10 '22 11:08 tinfoil-knight