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

Handle error response code when loading .riv files #297

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
14 changes: 11 additions & 3 deletions js/src/rive.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1193,7 +1193,12 @@ export class Rive {
): Promise<void> {
// Load the buffer from the src if provided
if (this.src) {
this.buffer = await loadRiveFile(this.src);
try {
this.buffer = await loadRiveFile(this.src);
} catch (e) {
this.eventManager.fire({ type: EventType.LoadError, data: e.message });
return Promise.reject(e.message);
}
}
// Load the Rive file
this.file = await this.runtime.load(new Uint8Array(this.buffer));
Expand Down Expand Up @@ -1920,8 +1925,11 @@ interface RiveFileContents {
const loadRiveFile = async (src: string): Promise<ArrayBuffer> => {
const req = new Request(src);
const res = await fetch(req);
const buffer = await res.arrayBuffer();
return buffer;
if (res.status < 400) {
const buffer = await res.arrayBuffer();
return buffer;
}
throw new Error(`Error loading ${src}: ${res.status} ${res.statusText}`);
};

// #endregion
Expand Down
29 changes: 29 additions & 0 deletions js/test/rive.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1163,3 +1163,32 @@ test("Rive deletes instances on the cleanup", (done) => {
});

// #endregion

// #region error handling

test("Rive triggers onLoadError if the request to load the .riv fails", (done) => {
global.fetch = jest
.fn()
.mockImplementation(() =>
Promise.resolve(
new Response("", { status: 404, statusText: "Not Found" })
)
);

const canvas = document.createElement("canvas");

new rive.Rive({
canvas: canvas,
src: "a/file.riv",
autoplay: true,
artboard: "MyArtboard",
onLoadError: () => {
// @ts-ignore
global.fetch.mockClear();
delete global.fetch;
done();
},
});
});

// #endregion