Search documentation of Localize

# Localize.ParseError exception (Localize v0.39.0)

Copy Markdown View Source

Exception raised when a language tag, unit identifier, or MF2 message cannot be parsed.

For MF2 message parse errors, the exception carries structured source location information (`:offset`, `:line`, `:column`) describing where in the input the parser failed. `:line` and `:column` are 1-indexed and `:offset` is a 0-indexed byte offset into the input string. This information is intended for tooling — editor integrations, language servers, and CLI diagnostics — that need to map errors back to source positions.

For other uses (language tag / unit identifier parsing) the location fields may be `nil`.

The `:reason` field is a documented atom describing the parser failure category. The `:detail` field optionally carries additional context (for example, a NimbleParsec expectation string) and `:cause` carries an underlying exception when a higher-level parser has wrapped a lower-level one.

## Summary

### Types

reason()
t()

### Functions

line_column(input, offset)

Computes 1-indexed line and column for a byte offset into `input`.

## Types

# reason()

    @type reason() ::
      :unexpected_trailing_input
      | :unexpected_input
      | :incomplete_input
      | :invalid_message_format

# t()

    @type t() :: %Localize.ParseError{
      __exception__: term(),
      cause: Exception.t() | nil,
      column: pos_integer() | nil,
      detail: String.t() | nil,
      input: String.t() | nil,
      line: pos_integer() | nil,
      offset: non_neg_integer() | nil,
      reason: reason() | nil,
      rest: String.t() | nil
    }

## Functions

# line_column(input, offset)

    @spec line_column(String.t(), non_neg_integer()) :: {pos_integer(), pos_integer()}

Computes 1-indexed line and column for a byte offset into `input`.

### Arguments

- `input` is the source string.

- `offset` is a 0-indexed byte offset into `input`.

### Returns

- `{line, column}` where both are 1-indexed positive integers.

- If `offset` is out of bounds, returns the position of the last character (or `{1, 1}` for an empty input).

### Examples

    iex> Localize.ParseError.line_column("Hello\nworld", 0)
    {1, 1}

    iex> Localize.ParseError.line_column("Hello\nworld", 6)
    {2, 1}

    iex> Localize.ParseError.line_column("Hello\nworld", 9)
    {2, 4}