diff --git a/js/web/lib/onnxjs/model.ts b/js/web/lib/onnxjs/model.ts index f9a1b6e76089d..8e689626011be 100644 --- a/js/web/lib/onnxjs/model.ts +++ b/js/web/lib/onnxjs/model.ts @@ -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 { @@ -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 { diff --git a/js/web/test/e2e/browser-test-webgl.js b/js/web/test/e2e/browser-test-webgl.js index e503f38ae5735..974c81d064c89 100644 --- a/js/web/test/e2e/browser-test-webgl.js +++ b/js/web/test/e2e/browser-test-webgl.js @@ -6,3 +6,16 @@ it('Browser E2E testing - WebGL backend', async function() { await testFunction(ort, {executionProviders: ['webgl']}); }); + +it('Browser E2E testing - invalid buffer', async () => { + try { + await ort.InferenceSession.create( + new Uint8Array(Array.from({length: 100}, () => 42)), {executionProviders: ['webgl']}); + + // Should not reach here. + assert(false); + } catch (e) { + assert(e.message.includes('as ONNX format')); + assert(e.message.includes('as ORT format')); + } +});