libassimp
0.4.1

Handle failures

Route typed validation, resolution, import, and positional export failures without partial output.

Open Markdown
import { AssimpError, type AssimpFailureCode } from 'libassimp';

const remedy = (error: AssimpError): string => {
  const remedies: Record<AssimpFailureCode, string> = {
    NO_FILES: 'Supply an entry file.',
    UNSUPPORTED_FORMAT: 'Choose an edge compiled into this entry.',
    INVALID_OPTIONS: 'Correct the named option paths.',
    RESOLVE_FAILED: `Retry or replace the source for ${error.fileName ?? 'the sidecar'}.`,
    IMPORT_FAILED: 'Correct the entry bytes/name or supply a required sidecar.',
    EXPORT_FAILED: `Correct target ${error.formatIndex ?? 0} (${error.format ?? 'unknown'}).`,
    LIMIT_EXCEEDED: 'Split the input, request fewer post-processing steps, or use the native backend.',
    ENGINE_FAILED: 'Retry once; the Wasm runtime has been replaced. Report a repeat.',
    BACKEND_UNAVAILABLE: "Reinstall the native package or use backend: 'wasm'.",
    ABORTED: 'The host is shutting down; do not retry.',
    INTERNAL: 'Report a libassimp bug with the input.',
  };
  return remedies[error.code];
};

console.log(remedy(new AssimpError('NO_FILES', 'convert needs at least one input file.')));

INVALID_OPTIONS is raised before any engine work for unknown/inapplicable fields, wrong kinds, bounds, duplicate steps, conflicts, and empty or repeated input names. RESOLVE_FAILED preserves the exact requested fileName and the thrown/rejected value as cause.

Plural target failures carry zero-based formatIndex and canonical format; context that does not apply is absent, so 'formatIndex' in error is a reliable test. Results are atomic: if a later exporter fails, no earlier output is returned.

import { AssimpError, convertFormats } from 'libassimp';

try {
  await convertFormats(
    { name: 'model.glb', bytes: new Uint8Array() },
    { targets: [{ to: 'stl' }, { to: '3mf' }] },
  );
} catch (error) {
  if (error instanceof AssimpError) {
    console.error(error.code, error.formatIndex, error.format, error.fileName);
  }
}

LIMIT_EXCEEDED means the engine ran out of memory; the instance stays usable. Anything the engine throws that is not already typed becomes ENGINE_FAILED with the original value as cause. Wasm loading failures and calls after disposal are plain errors rather than conversion failures. A rejected queued conversion does not poison later jobs on the same live instance.