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

feat: CXSPA-6890 Create toggle and optimization options for propagating errors to the server #19021

Merged
merged 12 commits into from
Jul 15, 2024
Original file line number Diff line number Diff line change
@@ -1,39 +1,56 @@
import { TestBed } from '@angular/core/testing';
import { FeatureConfigService } from '@spartacus/core';
import { PROPAGATE_ERROR_TO_SERVER } from '../error-response/propagate-error-to-server';
import { PropagatingToServerErrorHandler } from './propagating-to-server-error-handler';

describe('PropagatingToServerErrorHandler', () => {
describe('default factories', () => {
let serverRespondingErrorHandler: PropagatingToServerErrorHandler;
let propagatingToServerErrorHandler: PropagatingToServerErrorHandler;
let featureConfigService: FeatureConfigService;
let propagateErrorResponse: any;

beforeEach(() => {
TestBed.configureTestingModule({
providers: [
PropagatingToServerErrorHandler,
FeatureConfigService,
{
provide: PROPAGATE_ERROR_TO_SERVER,
useValue: jest.fn(),
},
],
});

serverRespondingErrorHandler = TestBed.inject(
propagatingToServerErrorHandler = TestBed.inject(
PropagatingToServerErrorHandler
);
propagateErrorResponse = TestBed.inject(PROPAGATE_ERROR_TO_SERVER);
featureConfigService = TestBed.inject(FeatureConfigService);
});

afterEach(() => {
jest.clearAllMocks();
});

it('should propagate error', () => {
it('should propagate error when propagateErrorsToServer is enabled', () => {
jest
.spyOn(featureConfigService, 'isEnabled')
.mockImplementationOnce((val) => val === 'propagateErrorsToServer');
const error = new Error('test error');

serverRespondingErrorHandler.handleError(error);
propagatingToServerErrorHandler.handleError(error);

expect(propagateErrorResponse as jest.Mock).toHaveBeenCalledWith(error);
});

it('should not propagate error when propagateErrorsToServer is disabled', () => {
jest
.spyOn(featureConfigService, 'isEnabled')
.mockImplementationOnce((val) => !(val === 'propagateErrorsToServer'));
const error = new Error('test error');

propagatingToServerErrorHandler.handleError(error);

expect(propagateErrorResponse as jest.Mock).not.toHaveBeenCalled();
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
*/

import { Injectable, inject } from '@angular/core';
import { MultiErrorHandler } from '@spartacus/core';
import { FeatureConfigService, MultiErrorHandler } from '@spartacus/core';
import { PROPAGATE_ERROR_TO_SERVER } from '../error-response/propagate-error-to-server';

/**
Expand All @@ -28,8 +28,13 @@ import { PROPAGATE_ERROR_TO_SERVER } from '../error-response/propagate-error-to-
})
export class PropagatingToServerErrorHandler implements MultiErrorHandler {
protected propagateErrorToServer = inject(PROPAGATE_ERROR_TO_SERVER);
private featureConfigService: FeatureConfigService =
inject(FeatureConfigService);

handleError(error: unknown): void {
if (!this.featureConfigService.isEnabled('propagateErrorsToServer')) {
return;
}
this.propagateErrorToServer(error);
}
}
1 change: 1 addition & 0 deletions core-libs/setup/ssr/optimized-engine/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

export * from './optimized-ssr-engine';
export * from './rendering-cache';
export * from './rendering-cache.model';
export * from './rendering-strategy-resolver';
export * from './rendering-strategy-resolver-options';
export { RequestContext, getRequestContext } from './request-context';
Expand Down
Loading
Loading