Playground recipe · recovery

Turn invalid input into evidence.

A malformed envelope does not leave the instance half-loaded. It becomes a normal message whose payload preserves the error and original response.

js-message works with bundlers and without a bundler.

Bundlers resolve the bare js-message import normally. Native browser ESM resolves it through a standard import map, with no build or transpilation step.

Complete source

import Message from 'js-message';

const incoming = new Message('{not valid json');

console.log('type', incoming.type);
console.error('error', incoming.data.err);
console.log('envelope', incoming.toJSON());

What to inspect

  • The resulting type is error.
  • data.err remains an Error in memory.
  • The JSON form retains its error name and message when the response is JSON-safe.