This page documents the 0.4 release candidates of @nexusdi/core. The newest on npm is 0.4.0-rc.1. The documentation for 0.3.2, the current release, is at nexus.js.org. Try the release candidate.
NEXUS_DEVTOOLS_GRAPH_INVALID
parseGraph(value) throws NEXUS_DEVTOOLS_GRAPH_INVALID when value is not a NexusGraph. parseGraph checks every field of a graph read back with JSON.parse, and the error’s path names the first field that failed. The call throws synchronously and bare. nexusdi graph from @nexusdi/cli calls parseGraph for a .json entry, and for a file that holds no graph it prints nexusdi: <file>: followed by this error’s message, then exits with code 2. A .json file whose text is invalid JSON fails earlier with a different message that carries no code.
Reproduce NEXUS_DEVTOOLS_GRAPH_INVALID
parseGraph throws NEXUS_DEVTOOLS_GRAPH_INVALID for a saved graph whose first provider has lost its token field. @nexusdi/devtools writes this message itself, so it carries its full text and fix line with no text pack.
import { Token, defineModule, isNexusError, provide } from '@nexusdi/core';
import { inspect, parseGraph } from '@nexusdi/devtools';
interface IReactorCore {
readonly output: number;
}
const REACTOR = new Token<IReactorCore>('ReactorCore');
class FusionReactor implements IReactorCore {
readonly output = 1.21;
}
const Engineering = defineModule({
name: 'Engineering',
providers: [provide(REACTOR, { useClass: FusionReactor })],
});
// A saved graph whose first provider lost its token, as a hand edit would.
const saved = JSON.parse(JSON.stringify(inspect(Engineering)));
delete saved.providers[0].token;
let path: string | null = null;
let message = '';
try {
parseGraph(saved);
} catch (error) {
if (!isNexusError(error, 'NEXUS_DEVTOOLS_GRAPH_INVALID')) throw error;
path = error.path;
message = error.message;
}
const failed = path; // -> 'providers[0].token'
console.log(failed);
const lines = message.split('\n'); // -> ['[NEXUS_DEVTOOLS_GRAPH_INVALID] not a NexusGraph: providers[0].token is missing or has the wrong type.', ' Fix: pass JSON.parse of the text that JSON.stringify(graph(ship)), JSON.stringify(inspect(root)) or nexusdi graph --format json wrote.']
console.log(lines.join('\n'));Try it: run npm install @nexusdi/core@next and npm install -D @nexusdi/devtools@next, save the region as parse.ts and run npx tsx parse.ts. It prints the path, providers[0].token, then the two-line message: [NEXUS_DEVTOOLS_GRAPH_INVALID] not a NexusGraph: providers[0].token is missing or has the wrong type. and its Fix: line, which starts Fix: pass JSON.parse of the text that.
Fix NEXUS_DEVTOOLS_GRAPH_INVALID
NEXUS_DEVTOOLS_GRAPH_INVALID goes away when parseGraph receives what graph(ship) or inspect(root) wrote, unedited. Write the file with JSON.stringify(graph(ship)) or nexusdi graph --format json, and pass JSON.parse of its text. JSON.stringify(inspect(root)) writes the same shape without a container.
import { Token, defineModule, provide } from '@nexusdi/core';
import { inspect, parseGraph } from '@nexusdi/devtools';
interface IReactorCore {
readonly output: number;
}
const REACTOR = new Token<IReactorCore>('ReactorCore');
class FusionReactor implements IReactorCore {
readonly output = 1.21;
}
const Engineering = defineModule({
name: 'Engineering',
providers: [provide(REACTOR, { useClass: FusionReactor })],
});
const text = JSON.stringify(inspect(Engineering));
const token = parseGraph(JSON.parse(text)).providers[0]?.token; // -> 'ReactorCore'
console.log(token);Try it: save the region as fixed.ts and run npx tsx fixed.ts. It prints ReactorCore. Change JSON.parse(text) to JSON.parse(text.replace('"token"', '"tokens"')) and run it again. npx tsc accepts the file, and npx tsx fixed.ts throws an uncaught DevtoolsError and prints a stack trace headed DevtoolsError: [NEXUS_DEVTOOLS_GRAPH_INVALID] not a NexusGraph: providers[0].token is missing or has the wrong type. The error object ends with path: 'providers[0].token'. Change the call back and the program prints ReactorCore again.
Fields of DevtoolsError
code:'NEXUS_DEVTOOLS_GRAPH_INVALID'or'NEXUS_DEVTOOLS_UNREGISTERED'.path: the first field that failed, such as'providers[3].module'.parseGraphchecks the modules first, including the types of theirimportsandexports, then the providers, then the edges, then the ids that each module’simportsandexportsname, so the first failure in that order wins.'(root)'names a value that is no object, and a top-level name such as'modules'names a missing list. A message that endsis missing or has the wrong typecovers a missing field or a field of the wrong type, and a message that endswhich no module hascovers an id that points at nothing, such asproviders[0].module. An id that two entries use fails at the second one, such as'modules[1].id', and the message names the path of the first owner. Module ids and provider ids share one namespace. An edge that points at nothing endswhich no provider has, and anexportsentry that points at nothing endswhich no module or provider has.nullfor NEXUS_DEVTOOLS_UNREGISTERED.