You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
If you received back the below message, then go to the JavaScript failure section:
InternalServerError
If the body contained a custom message, or nothing at all, run the below query in Log Explorer after setting the time range:
selectconsole_logs.event_message,
cast(invocation_events.timestampas datetime) astimestamp,
invocation_events.function_namefrom
function_logs as console_logs
left join UNNEST(console_logs.metadata) as metadata on true
left join (
selecttimestamp,
em.execution_id,
res.status_code,
req.pathnameas function_name
from
function_edge_logs
left join UNNEST(metadata) as em on true
left join UNNEST(em.request) as req on true
left join UNNEST(em.response) as res on true
) as invocation_events
onmetadata.execution_id=invocation_events.execution_idwhereinvocation_events.status_code=500andmetadata.level='error'andmetadata.event_typein ('Log', 'UncaughtException')
andconsole_logs.event_messagelike'%Error:%file:///%'order byinvocation_events.function_name, invocation_events.timestamplimit50;
The function's log will produce an event_message with the error type. It may look like:
TypeError: Cannot read properties of undefined (reading 'some_func')
at Object.handler (file:///var/tmp/sb-compile-edge-runtime/source/index.ts:15:26)
at eventLoopTick (ext:core/01_core.js:175:7)
at async mapped (ext:runtime/http.js:246:20)
The first line tells you the error type and message. The stack trace points to the file and line number.
The Mozilla Foundation documents all error objects and what they mean:
However, you can also review the below example cases for an idea of possible causes.
Example cases
TypeError: Undefined variables
A TypeError occurs when any JavaScript datatype is misused. For instance, trying to execute a number as if it were a function would cause the error:
constsome_num=5some_num()// TypeError: some_num is not a function
This issue often appears when working with returned objects from external APIs. One may assume a response has a certain shape, but if the value is actually null or undefined, using it without checking can lead to a TypeError.
constdata=awaitreq.json()// returns undefined if request body is emptydata.some_obj.some_val// TypeError: Cannot read properties of undefined
Fix 1: Type-check before using potentially unknown values:
const{ user_submission }=awaitreq.json()// checking value for appropriate datatypeif(typeofuser_submission==='undefined'){returnnewResponse(JSON.stringify({message: 'Submission is empty. Please try again.'}),{headers: { ...corsHeaders,'Content-Type': 'application/json'},status: 400,})}// rest of code ...
try{some_obj.some_func();// TypeError: Cannot read properties of undefined
...
}catch(err){// customize the error messageconsole.error('return object was misformatted:',err)}finally{// add a custom error response for easier debuggingreturnnewResponse(JSON.stringify({message: 'Could not parse return object'}),{headers: { ...corsHeaders,'Content-Type': 'application/json'},status: 500// opt to customize the status code to better fit the situation})}
ReferenceError: Var is not defined
A ReferenceError occurs when one tries to reference a variable that does not exist in the code's scope. Often times caused by a typo or missing import.
For instance, if one tries to access a variable before it is defined, they will encounter the error:
leta=some_uninitialized_var// ReferenceError: some_uninitialized_var is not defined...
Fix:
Check the variable name in the error message against your code to ensure there are no typos
Make sure the variable is declared before it's used
If it's from a package, confirm the import exists and the export name is correct
If the error references a JavaScript internal, make sure it is compatible with the Supabase Runtime. If not, consider refactoring or updating the library's version
Custom errors
You explicitly threw an error somewhere in your code, or a third-party package did:
thrownewError('custom, unhandled error')
Alternatively, in a try/catch blocks, you may have augmented the standard error message:
try{// induce reference errorconsta=unitialized_var// ReferenceError...}catch(error){console.error('custom error message...',error)// modifying the original error message}
When you customize the error response, it's important to define an appropriate new message. It may also be worthwhile changing the default 500 code returned during errors to a value more reflective of the situation for easier debugging in the future:
...
catch(error){console.error('custom error message...',error)// modifying the original error messagereturnnewResponse(JSON.stringify({message: 'Permissions error, please sign in'}),{headers: { ...corsHeaders,'Content-Type': 'application/json'},status: 401// customizing the status code})}
SyntaxError: Special case - CORS violation
A SyntaxError error occurs when Deno's grammatical rules are violated, such as failing to close a parenthesis:
console.log('unclosed';// Uncaught SyntaxError: missing ) after argument list
In most cases, syntax violations can be fixed by removing a typo. There is a special case that is common enough that it is worth providing an example over: CORS violations.
When making calls from a browser, such as FireFox or Chrome, the site will make an OPTIONS request before sending over the actual payload. This is a security mechanism done to prevent Cross-Site-Request-Forgery attacks. To satisfy the request, you need to have a CORS handler in place:
Without the OPTIONS handler, requests from the browser will be misinterpreted, resulting in an Unexpected end of JSON input error log.
SyntaxError: Unexpected end of JSON input
at parse (<anonymous>)
at packageData (ext:deno_fetch/22_body.js:408:14)
at consumeBody (ext:deno_fetch/22_body.js:261:12)
at eventLoopTick (ext:core/01_core.js:175:7)
at async Object.handler (file:///var/tmp/sb-compile-edge-runtime/source/index.ts:5:20)at async mapped (ext:runtime/http.js:246:20)
The solution is to follow our guide on adding CORS support.
It is also important to note that if any error occurs before the CORS check can be satisfied, the browser may falsely report CORS as the reason a request failed:
// returns before the CORS check can be satisfiedreturnif(req.method==='OPTIONS'){returnnewResponse('ok',{headers: corsHeaders})}
So, when encountering these errors, it is still important to check the logs or run the request outside the browser to make sure it is actually the primary factor and not a side-effect of a larger issue.
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
Uh oh!
There was an error while loading. Please reload this page.
-
This is a copy of a troubleshooting article on Supabase's docs site. It may be missing some details from the original. View the original article.
A 500 from an Edge Function means one of two things.
Quick triage
If you received back the below message, then go to the JavaScript failure section:
If the body contained a custom message, or nothing at all, run the below query in Log Explorer after setting the time range:
Based on the output, go to the relevant section:
Your custom response returned a 500
Somewhere in your function logic, you are returning a 500 response yourself:
Example:
Fix:
console.error()message before the code returns, so future occurrences leave a better trace.See: Error handling in Edge Functions
JavaScript failure
An unhandled JavaScript Error emerged during execution.
The function's log will produce an
event_messagewith the error type. It may look like:The first line tells you the error type and message. The stack trace points to the file and line number.
The Mozilla Foundation documents all error objects and what they mean:
ErrorAggregateErrorEvalErrorRangeErrorReferenceErrorSuppressedErrorSyntaxErrorTypeErrorURIErrorInternalErrorHowever, you can also review the below example cases for an idea of possible causes.
Example cases
TypeError: Undefined variables
A
TypeErroroccurs when any JavaScript datatype is misused. For instance, trying to execute a number as if it were a function would cause the error:This issue often appears when working with returned objects from external APIs. One may assume a response has a certain shape, but if the value is actually
nullorundefined, using it without checking can lead to aTypeError.Fix 1: Type-check before using potentially unknown values:
Fix 2: Wrap problematic code in try/catch:
One could use a try/catch/finally block to handle these errors:
ReferenceError: Var is not defined
A
ReferenceErroroccurs when one tries to reference a variable that does not exist in the code's scope. Often times caused by a typo or missing import.For instance, if one tries to access a variable before it is defined, they will encounter the error:
Fix:
Custom errors
You explicitly threw an error somewhere in your code, or a third-party package did:
Alternatively, in a try/catch blocks, you may have augmented the standard error message:
When you customize the error response, it's important to define an appropriate new message. It may also be worthwhile changing the default
500code returned during errors to a value more reflective of the situation for easier debugging in the future:SyntaxError: Special case - CORS violation
A
SyntaxErrorerror occurs when Deno's grammatical rules are violated, such as failing to close a parenthesis:In most cases, syntax violations can be fixed by removing a typo. There is a special case that is common enough that it is worth providing an example over: CORS violations.
When making calls from a browser, such as FireFox or Chrome, the site will make an OPTIONS request before sending over the actual payload. This is a security mechanism done to prevent Cross-Site-Request-Forgery attacks. To satisfy the request, you need to have a CORS handler in place:
Without the
OPTIONShandler, requests from the browser will be misinterpreted, resulting in anUnexpected end of JSON inputerror log.SyntaxError: Unexpected end of JSON input at parse (<anonymous>) at packageData (ext:deno_fetch/22_body.js:408:14) at consumeBody (ext:deno_fetch/22_body.js:261:12) at eventLoopTick (ext:core/01_core.js:175:7) at async Object.handler (file:///var/tmp/sb-compile-edge-runtime/source/index.ts:5:20)at async mapped (ext:runtime/http.js:246:20)The solution is to follow our guide on adding CORS support.
It is also important to note that if any error occurs before the CORS check can be satisfied, the browser may falsely report CORS as the reason a request failed:
So, when encountering these errors, it is still important to check the logs or run the request outside the browser to make sure it is actually the primary factor and not a side-effect of a larger issue.
Still stuck?
Beta Was this translation helpful? Give feedback.
All reactions