cli
cli copied to clipboard
Netlify dev tries to resolve to a page when returning 404 from a function
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:
- Create a function that returns a 404 error
- Execute the function in netlify dev
- 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.
Reported also in https://github.com/netlify/cli/issues/695#issuecomment-725626061
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!
@digitpain Do you have any public repository that can be shared so I can reproduce the issue?
If it helps this is the repo I was referencing in the original issue: https://github.com/ercgrat/creator.urbanbasic.de
@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](https://user-images.githubusercontent.com/51631122/183286520-a54e894e-6fcd-4c5a-bf6f-af8c4e1ab83d.png)
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),
};
});
@danez I think this issue can be closed.