Charts
API reference

Errors

Every error a script can see or cause, as typed unions.

Errors are discriminated unions with a type field. Handle them with an exhaustive switch.

Your data sources

Passed to the poll or websocket handler as { type: "Error", error }.

/** Why a user data source delivered no value. Passed to the script's handler. */
export type DataError =
  | { readonly type: "RequestFailed"; readonly reason: string }
  | { readonly type: "HttpStatus"; readonly status: number }
  | { readonly type: "InvalidJson"; readonly reason: string }
  /** `message` is the schema's validation message. */
  | { readonly type: "SchemaMismatch"; readonly message: string }
  | { readonly type: "SocketClosed"; readonly code: number; readonly reason: string };

Streams

In a view's status as Unavailable.

/** Why a market or platform stream stopped. Shown in the script's legend entry. */
export type StreamError =
  | { readonly type: "SymbolNotFound"; readonly symbol: string }
  | { readonly type: "TimeframeNotSupported"; readonly timeframe: Timeframe }
  | { readonly type: "HistoryFailed"; readonly reason: string }
  | { readonly type: "Disconnected"; readonly reason: string }
  | { readonly type: "NoAccess"; readonly reason: string };

Sessions

From SymbolInfo.sessionOf.

/** Why `SymbolInfo.sessionOf` has no session. */
export type SessionError =
  | { readonly type: "NoSessionCalendar"; readonly symbol: string }
  | { readonly type: "InvalidTime"; readonly time: number };

Script errors

Shown in the indicator list and the editor. Scripts do not handle these: the runtime catches throws and reports them.

/**
 * Everything that can go wrong with a running script. The runtime catches
 * throws from script code, so none of these crash the chart. Shown in the
 * legend and the script console.
 */
export type ScriptError =
  /** The module failed to import or has no `defineScript` default export. */
  | { readonly type: "LoadFailed"; readonly reason: string }
  /**
   * Script code threw. `name` is the stream of a handler or the event
   * handler, e.g. `"onDrag"`. The script keeps running; a throw in `state`
   * or `subscribe` stops it.
   */
  | {
      readonly type: "ScriptThrew";
      readonly entry: ScriptEntry;
      readonly name: string | null;
      readonly error: unknown;
    }
  | { readonly type: "StreamFailed"; readonly stream: string; readonly error: StreamError }
  /** A value of a user data stream did not match its schema. The stream keeps running. */
  | { readonly type: "InvalidData"; readonly stream: string; readonly message: string }
  | { readonly type: "InvalidInput"; readonly error: InputError };
/** A saved input value that does not fit its input. The default is used instead. */
export type InputError =
  | {
      readonly type: "WrongType";
      readonly input: string;
      readonly expected: InputKind;
      readonly value: unknown;
    }
  | {
      readonly type: "OutOfRange";
      readonly input: string;
      readonly value: number;
      readonly min: number | undefined;
      readonly max: number | undefined;
    }
  | { readonly type: "UnknownOption"; readonly input: string; readonly value: string };

On this page