Skip to content

[Live] Adding a response:error hook #587

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

Merged
merged 1 commit into from
Dec 5, 2022
Merged
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
11 changes: 9 additions & 2 deletions src/LiveComponent/assets/src/Component/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ export default class Component {
* * disconnect (component: Component) => {}
* * render:started (html: string, response: BackendResponse, controls: { shouldRender: boolean }) => {}
* * render:finished (component: Component) => {}
* * response:error (backendResponse: BackendResponse, controls: { displayError: boolean }) => {}
* * loading.state:started (element: HTMLElement, request: BackendRequest) => {}
* * loading.state:finished (element: HTMLElement) => {}
* * model:set (model: string, value: any, component: Component) => {}
Expand Down Expand Up @@ -145,7 +146,7 @@ export default class Component {
return this.valueStore.get(modelName);
}

action(name: string, args: any, debounce: number|boolean = false): Promise<BackendResponse> {
action(name: string, args: any = {}, debounce: number|boolean = false): Promise<BackendResponse> {
const promise = this.nextRequestPromise;
this.pendingActions.push({
name,
Expand Down Expand Up @@ -301,10 +302,16 @@ export default class Component {
const backendResponse = new BackendResponse(response);
thisPromiseResolve(backendResponse);
const html = await backendResponse.getBody();

// if the response does not contain a component, render as an error
const headers = backendResponse.response.headers;
if (headers.get('Content-Type') !== 'application/vnd.live-component+html' && !headers.get('X-Live-Redirect')) {
this.renderError(html);
const controls = { displayError: true };
this.hooks.triggerHook('response:error', backendResponse, controls);

if (controls.displayError) {
this.renderError(html);
}

return response;
}
Expand Down
35 changes: 34 additions & 1 deletion src/LiveComponent/assets/test/controller/error.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@

import { createTest, initComponent, shutdownTests } from '../tools';
import { getByText, waitFor } from '@testing-library/dom';
import BackendResponse from '../../src/BackendResponse';

const getErrorElement = (): Element|null => {
return document.getElementById('live-component-error');
};

describe('LiveController Error Handling', () => {
afterEach(() => {
Expand Down Expand Up @@ -39,7 +44,7 @@ describe('LiveController Error Handling', () => {
await waitFor(() => expect(document.getElementById('live-component-error')).not.toBeNull());
// the component did not change or re-render
expect(test.element).toHaveTextContent('Original component text');
const errorContainer = document.getElementById('live-component-error');
const errorContainer = getErrorElement();
if (!errorContainer) {
throw new Error('containing missing');
}
Expand Down Expand Up @@ -69,4 +74,32 @@ describe('LiveController Error Handling', () => {
// the component did not change or re-render
expect(test.element).toHaveTextContent('Original component text');
});

it('triggers response:error hook', async () => {
const test = await createTest({ }, (data: any) => `
<div ${initComponent(data)}>
component text
</div>
`);

test.expectsAjaxCall('post')
.expectSentData(test.initialData)
.serverWillReturnCustomResponse(200, `
<html><head><title>Hi!</title></head><body><h1>I'm a whole page, not a component!</h1></body></html>
`)
.expectActionCalled('save')
.init();

let isHookCalled = false;
test.component.on('response:error', (backendResponse: BackendResponse, controls) => {
isHookCalled = true;
controls.displayError = false;
});

await test.component.action('save');

await waitFor(() => expect(isHookCalled).toBe(true));
const errorContainer = getErrorElement();
expect(errorContainer).toBeNull();
});
});
1 change: 1 addition & 0 deletions src/LiveComponent/doc/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -471,6 +471,7 @@ The following hooks are available (along with the arguments that are passed):
* ``disconnect`` args ``(component: Component)``
* ``render:started`` args ``(html: string, response: BackendResponse, controls: { shouldRender: boolean })``
* ``render:finished`` args ``(component: Component)``
* ``response:error`` args ``(backendResponse: BackendResponse, controls: { displayError: boolean })``
* ``loading.state:started`` args ``(element: HTMLElement, request: BackendRequest)``
* ``loading.state:finished`` args ``(element: HTMLElement)``
* ``model:set`` args ``(model: string, value: any, component: Component)``
Expand Down