# RenderError

The error thrown when Mermaid source cannot be rendered.

`ondine/compiler` and `ondine/compiler/wasm` both export `RenderError`. Its message matches the Mermaid diagnostic for the same input, and its fields give the location of the problem in the source.

```ts title="build.ts"
import { render, RenderError } from 'ondine/compiler'

try {
  render('flowchart LR\n  A -->')
} catch (error) {
  // 'syntax' 3 1
  if (error instanceof RenderError) console.log(error.code, error.line, error.column)
}
```

## Signature

```ts
// The library constructs this error, and render throws it
class RenderError extends Error
```

`name` is `'Render.RenderError'`. Use `instanceof` to identify this error.

## Properties

### code

* **Type:** `'limit' | 'semantic' | 'syntax' | 'unsupported-diagram'`

The kind of failure.

| Code                  | Meaning                                                                            |
| --------------------- | ---------------------------------------------------------------------------------- |
| `limit`               | The source, edge count, or output exceeds a `render` limit.                        |
| `semantic`            | The source parses but describes an invalid diagram, such as an invalid gantt date. |
| `syntax`              | The source does not parse.                                                         |
| `unsupported-diagram` | No diagram type is detected, or the type is not ported yet.                        |

```ts
import { render, RenderError } from 'ondine/compiler'

try {
  render('flowchart LR\n  A --> B', { limit: 5 })
} catch (error) {
  // 'limit'
  if (error instanceof RenderError) console.log(error.code)
}
```

### column

* **Type:** `number`

The one-based UTF-16 column, or `0` if the error has no position.

```ts
// Points an editor cursor at the error
const offset = error.column > 0 ? error.column - 1 : 0
```

### line

* **Type:** `number`

The one-based line, or `0` if the error has no position.

```ts
// Formats a compiler-style location
const location = error.line > 0 ? `flow.mmd:${error.line}:${error.column}` : 'flow.mmd'
```

### message

* **Type:** `string`

The Mermaid diagnostic text. This text can have more than one line.

```ts
// 'Diagram type "agentflow" is not supported yet.'
console.log(error.message)
```

### snippet

* **Type:** `string`

The source line with the error and a caret below the column, or an empty string.

```ts
// Prints the line and a caret below it
if (error.snippet) console.error(error.snippet)
```

### type

* **Type:** `string | undefined`

The detected diagram type, if the compiler detected the type before the failure. Otherwise, the value is `undefined`.

```ts
// 'flowchart' for a flowchart syntax error, undefined for undetected text
console.log(error.type)
```
