# Function: Troubleshooting

## Table of Contents

- [Error handling](#error-handling)
- [Resource errors](#resource-errors)
- [Function-specific errors](#function-specific-errors)
- [Fixing resource limit errors](#fixing-resource-limit-errors)
- [General debugging](#general-debugging)
- [See also](#see-also)

---

## Error handling

When a function throws, the result comes back with `isFulfilled: false` and the error details at `result.value`:

```js
importcreateClientfrom'microlink.io'

constmicrolink=createClient()

constfailing=({name})=>name()

constresult=awaitmicrolink.function('https://example.com',failing,{

name:'Kiko'

})

console.log(result.isFulfilled)// false

console.log(result.value.name)// 'TypeError'

console.log(result.value.message)// 'name is not a function'
```

The promise only rejects when the API call itself fails — an invalid URL, an expired API key, a timeout — and then it throws a [MicrolinkError](https://microlink.io/docs/sdk/getting-started/errors) like every other SDK method.

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 5120 bytes free plan limit                    |
| `ConcurrencyError`     | The same function is already running against the same URL without a key |
| `OutgoingRequestError` | Function made a cross-origin network request on the free plan           |

Each error message is plan-aware:

```json
{

"isFulfilled":false,

"value":{

"name":"TimeoutError",

"message":"Function exceeded the 10s free plan timeout. Upgrade to pro for up to 60s with retries."

}

}
```

## 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 (10s free, 60s pro):

1.  Reduce the function to a trivial check such as `({ page }) => page.title()` to confirm the page itself loads in time.
2.  Drop anything else from the request — a function asked for on its own skips metadata, while adding `screenshot`, `pdf`, `insights`, `iframe`, `video`, `audio`, `palette` or a `data` rule brings it back.
3.  Replace fixed waits with `waitForSelector` whenever possible.
4.  Move heavy post-processing to your own server.

**CpuTimeError** — the function used too much CPU time:

1.  Simplify the computation or break it into smaller steps.
2.  Avoid CPU-intensive operations like large JSON parsing or complex regular expressions.
3.  Move heavy processing to your own server and use the function only for data extraction.

**MemoryError** — the function exceeded its memory limit (64 MB free, 128 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`:

1.  Reduce the amount of data held in memory at once.
2.  Avoid loading entire pages into memory when you only need a small part.
3.  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 5120 bytes free plan limit:

1.  Shrink the function: drop unused code and move large constants into function arguments. Compression does not help, because the limit applies to the decompressed code.
2.  Upgrade to pro for unlimited code size.

**ConcurrencyError** — the same function is already running twice against the same URL. On the free plan, up to 2 copies of a function run per URL at a time, whatever IP address or free API key sends them:

1.  Wait for the current function execution to finish before sending it again.
2.  Upgrade to a paid plan, which doesn't have this limit.

**OutgoingRequestError** — the function made a cross-origin network request on the free plan:

1.  Remove any `fetch`, `XMLHttpRequest`, or WebSocket calls to external domains.
2.  Same-origin requests (relative URLs or same host as the target URL) are always allowed.
3.  Upgrade to pro for unrestricted outgoing requests.

## General debugging

1.  Start simple — reduce the function to `({ page }) => page.title()` to isolate whether the problem is in your code or the target page.
2.  Check what else you asked for — a function on its own skips metadata, while `screenshot`, `pdf`, `insights`, `iframe`, `video`, `audio`, `palette` or a `data` rule keeps it on.
3.  Check profiling — inspect `result.profiling` to understand where time is being spent.
4.  Use the right context — keep orchestration in the outer function and DOM-only code inside `page.evaluate`.
5.  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](https://microlink.io/docs/guides/function/profiling-and-performance) — understand execution phases and optimize.
- [Common troubleshooting](https://microlink.io/docs/guides/common/troubleshooting) — timeouts, blocked sites, auth issues, and debug headers that apply to every workflow.
- [Function reference](https://microlink.io/docs/api/parameters/function) — response shape, plan limits, and resource errors.