Really like netlify functions. Yet I’m wondering what’s the recommended/best way to handle errors in lambda functions. Can we just throw new Error in the lambda and .catch it in the client ?
You don’t need to ‘throw’ an error in Lamda. You can just do something like:
fetch('/.netlify/functions/foo/', {method: 'POST'}).then(response => {
if (response.ok) {
return response // or response.json()
} else {
throw response.statusText
}).then(data => {
// process the data here
}).catch(error => console.log(error))
Anything that won’t return a valid response will automatically be treated as an error. In most cases, it will return a 500 error. However, if you want the complete error text, you could return that error as a JSON string and use it client-side.