Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[js] Make error friendly when isOrtFormat is undefined #19958

Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions js/node/test/unittests/lib/inference-session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,11 @@ describe('UnitTests - InferenceSession.create()', () => {
await InferenceSession.create(new Uint8Array(0));
}, {name: 'Error', message: /No graph was found in the protobuf/});
});
it('EXPECTED FAILURE - invalid buffer', async () => {
await assert.rejects(async () => {
await InferenceSession.create(new Uint8Array(Array.from({length: 100}, () => 42)));
}, {name: 'Error', message: /as ONNX.+as ORT/m});
});
sevenc-nanashi marked this conversation as resolved.
Show resolved Hide resolved
// #endregion

it('metadata: inputNames', async () => {
Expand Down
12 changes: 11 additions & 1 deletion js/web/lib/onnxjs/model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export class Model {
constructor() {}

load(buf: Uint8Array, graphInitializer?: Graph.Initializer, isOrtFormat?: boolean): void {
let onnxError: Error|undefined;
if (!isOrtFormat) {
// isOrtFormat === false || isOrtFormat === undefined
try {
Expand All @@ -25,10 +26,19 @@ export class Model {
if (isOrtFormat !== undefined) {
throw e;
}
onnxError = e;
}
}

this.loadFromOrtFormat(buf, graphInitializer);
try {
this.loadFromOrtFormat(buf, graphInitializer);
} catch (e) {
if (isOrtFormat !== undefined) {
throw e;
}
// Tried both formats and failed (when isOrtFormat === undefined)
throw new Error(`Failed to load model as ONNX format: ${onnxError}\nas ORT format: ${e}`);
}
}

private loadFromOnnxFormat(buf: Uint8Array, graphInitializer?: Graph.Initializer): void {
Expand Down