Skip to content

Commit

Permalink
feat: pass script loading error to the ErrorHandler
Browse files Browse the repository at this point in the history
  • Loading branch information
arturovt committed Jun 15, 2021
1 parent 98793ba commit 9999545
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,6 @@ describe('LazyElementDirective', () => {
await fixture.whenStable();
fixture.detectChanges();

expect(consoleErrorSpy).toHaveBeenCalledTimes(1);
expect(consoleErrorSpy).toHaveBeenCalledWith(
'@angular-extensions/elements - Loading of element <some-element> failed, please provide <ng-template #error>Loading failed...</ng-template> and reference it in *axLazyElement="errorTemplate: error" to display customized error message in place of element'
);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { ErrorHandler } from '@angular/core';
import { TestBed, waitForAsync } from '@angular/core/testing';

import {
Expand Down Expand Up @@ -378,4 +379,24 @@ describe('LazyElementsLoaderService preconfigured with LazyElementsModule', () =
});
});
});

describe('ErrorHandler', () => {
it('should be possible to handle the error thrown during the script loading', (done) => {
shouldLoadSucceed = false;

const errorHandler = TestBed.inject(ErrorHandler);
const handleErrorSpy = spyOn(errorHandler, 'handleError');

const promise = service.loadElement(
'http://elements.com/some-element',
'some-element'
);

promise
.catch(() => {
expect(handleErrorSpy).toHaveBeenCalledTimes(1);
})
.finally(done);
});
});
});
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
import { Inject, Injectable, Optional, Type } from '@angular/core';
import {
ErrorHandler,
Inject,
Injectable,
Optional,
Type,
} from '@angular/core';

import { LazyElementRootOptions } from './lazy-elements.module';
import {
Expand Down Expand Up @@ -35,6 +41,7 @@ export class LazyElementsLoaderService {
configs: ElementConfig[] = [];

constructor(
private errorHandler: ErrorHandler,
@Inject(LAZY_ELEMENTS_REGISTRY) private registry: LazyElementsRegistry,
@Optional()
@Inject(LAZY_ELEMENT_ROOT_OPTIONS)
Expand Down Expand Up @@ -155,6 +162,10 @@ export class LazyElementsLoaderService {
const onError = (error: ErrorEvent) => {
notifier.reject(error);
cleanup();
// Caretaker note: don't put it before the `reject` and `cleanup` since the user may have some
// custom error handler that will re-throw the error through `throw error`. Hence the code won't
// be executed, and the promise won't be rejected.
this.errorHandler.handleError(error);
};
// The `load` and `error` event listeners capture `this`. That's why they have to be removed manually.
// Otherwise, the `LazyElementsLoaderService` is not going to be GC'd.
Expand Down

0 comments on commit 9999545

Please sign in to comment.