Function: Troubleshooting
Error handling
When a function throws, the result comes back with
isFulfilled: false and the error details at result.value:const microlink = require('@microlink/function')
const failing = ({ name }) => name()
const fn = microlink(failing)
const result = await fn('https://example.com', { name: 'Kiko' })
console.log(result.isFulfilled) // false
console.log(result.value.name) // 'TypeError'
console.log(result.value.message) // 'name is not a function'Non-Error throws (like
throw 'oh no') are normalized into a NonError with the thrown value as the message.Resource errors
When a function exceeds its plan limits, the API returns a descriptive error instead of failing the entire request:
| Error | Trigger |
|---|---|
TimeoutError | Function wall-clock time exceeded the plan limit |
CpuTimeError | Function CPU time exceeded the plan limit |
MemoryError | Function memory usage exceeded the plan limit |
CodeSizeError | Function code exceeds the 1024 bytes free plan limit |
ConcurrencyError | Too many concurrent function executions for the free plan (1 per IP) |
OutgoingRequestError | Function made a cross-origin network request on the free plan |
Each error message is plan-aware:
{
"isFulfilled": false,
"value": {
"name": "TimeoutError",
"message": "Function exceeded the 5s free plan timeout. Upgrade to pro for up to 28s."
}
}Function-specific errors
- EINVALFUNCTION — the function string has invalid JavaScript syntax. Check quotes, brackets, template strings, and arrow function formatting.
- EINVALEVAL — the function executed but threw at runtime. Check undefined variables, DOM queries that return null, or mistakes inside
page.evaluate.
Fixing resource limit errors
TimeoutError — the function exceeded its plan timeout (5s free, ~28s pro):
- Reduce the function to a trivial check such as
({ page }) => page.title()to confirm the page itself loads in time. - Set
meta: falseunless metadata is part of the requirement. - Replace fixed waits with
waitForSelectorwhenever possible. - Move heavy post-processing to your own server.
CpuTimeError — the function used too much CPU time:
- Simplify the computation or break it into smaller steps.
- Avoid CPU-intensive operations like large JSON parsing or complex regular expressions.
- Move heavy processing to your own server and use the function only for data extraction.
MemoryError — the function exceeded its memory limit (16 MB free, 32 MB pro). The limit applies to the JavaScript heap, which is where objects, arrays, and strings live, and it is reported as
profiling.memory.heap:- Reduce the amount of data held in memory at once.
- Avoid loading entire pages into memory when you only need a small part.
- Use streaming or pagination patterns when dealing with large datasets.
Binary data such as
Buffer and typed arrays is allocated off the heap and is reported separately as profiling.memory.external. It does not count toward this limit, so a function returning large screenshots or PDFs is unlikely to hit MemoryError.CodeSizeError — the function code exceeds the 1024 bytes free plan limit:
- Use the @microlink/functionlibrary, which compresses code automatically.
- Compress the function body manually with
lz#,br#, orgz#prefixes. - Upgrade to pro for unlimited code size.
ConcurrencyError — too many concurrent function executions (1 per IP on the free plan):
- Wait for the current function execution to finish before sending another request.
- Upgrade to pro for unlimited concurrency.
OutgoingRequestError — the function made a cross-origin network request on the free plan:
- Remove any
fetch,XMLHttpRequest, or WebSocket calls to external domains. - Same-origin requests (relative URLs or same host as the target URL) are always allowed.
- Upgrade to pro for unrestricted outgoing requests.
General debugging
- Start simple — reduce the function to
({ page }) => page.title()to isolate whether the problem is in your code or the target page. - Disable metadata — set
meta: falseunless metadata is part of the requirement. - Check profiling — inspect
result.profilingto understand where time is being spent. - Use the right context — keep orchestration in the outer function and DOM-only code inside
page.evaluate. - Watch for null — DOM queries like
document.querySelector()return null when the element doesn't exist. Always use optional chaining or null checks.
See also
- Profiling and performance — understand execution phases and optimize.
- Common troubleshooting — timeouts, blocked sites, auth issues, and debug headers that apply to every workflow.
- Function reference — response shape, plan limits, and resource errors.