> For the complete documentation index, see [llms.txt](https://racoon-js.gitbook.io/en/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://racoon-js.gitbook.io/en/other-common-api/error.md).

# error

error(message, \[ctx])

## Effect

Set the custom error message when validate failed.

By default, the error message is friendly only to the developer. If you want to use custom error message, call `error`.

## How To Use

You can call `error` any number of times you like. Every time you call `error`, it will set a custom error message for the *restrict* before the position you call `error`.

## Arguments

* `message` `(string | function)` - The custom error message. If it's a function, the function should return a string, respect the custom error message. *The function has an argument: the original validate error object.*
* `[ctx]` ***(\*)*** - The execution context of `message` when it's a function.&#x20;

> ATTENTION
>
> In the version 1.2.x and before, the function has an argument: the original validate error message string. It's a string, not an object.

## Example

```javascript
const schema = racoon
    .string()
    .error('Name should be a type of string')
    .required()
    .error('Name is required');
schema.validate(1); // fail, error message is 'Name should be a type of string'
schema.validate(null); // fail, error message is 'Name is required'
```
