Skip to content

fix(options): return explicit error on invalid JSON options #416

Description

@luojiyin1987

Problem

When users pass invalid JSON options to the Resvg constructor or renderAsync, the error is silently ignored and falls back to default options.

// Typo in option name - no error thrown
const resvg = new Resvg(svg, { dpii: 96 })

This makes debugging difficult as users don't know their options are being ignored.

Root Cause

In src/lib.rs:256-258 and src/lib.rs:391-393:

let js_options: JsOptions = options
    .and_then(|o| serde_json::from_str(o.as_str()).ok())
    .unwrap_or_default();

The .ok() call silently converts any serde error to None, then falls back to default.

Proposed Solution

Return explicit errors for:

  • JSON syntax errors
  • Unknown fields (already handled by deny_unknown_fields)
  • Invalid field values (e.g., negative numbers for u32)
  • Type mismatches

Error Examples

After fix:

  • Unknown field: Invalid options: unknown field 'dpii', expected one of 'font', 'dpi', ...
  • Invalid value: Invalid options: invalid value: integer '-1', expected u32 at line 1 column 25
  • JSON syntax: Invalid options: expected ':' at line 1 column 10

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions