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

[Angular] [Regression] Fix handling of not found layout service requests in Angular sample #809

Merged
merged 1 commit into from
Sep 9, 2021
Merged
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
16 changes: 15 additions & 1 deletion samples/angular/src/app/layout/jss-layout.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {
LayoutServiceContextData,
} from '@sitecore-jss/sitecore-jss-angular';
import { from as fromPromise, Observable, throwError as observableThrow } from 'rxjs';
import { catchError } from 'rxjs/operators';
import { catchError, map } from 'rxjs/operators';
import { layoutServiceFactory } from '../lib/layout-service-factory';

const layoutServiceInstance = layoutServiceFactory.create();
Expand All @@ -22,11 +22,25 @@ export class JssLayoutService {
language: string
): Observable<LayoutServiceData | LayoutServiceError> {
return fromPromise(layoutServiceInstance.fetchLayoutData(route, language)).pipe(
map(routeData => {
if (!routeData.sitecore.route) {
// A missing route value signifies an invalid path, so simulate Not Found error
const error = new LayoutServiceError();
error.status = 404;
error.statusText = 'Not Found';
error.data = routeData;
throw error;
}
return routeData;
}),
catchError(this.getLayoutServiceError)
);
}

private getLayoutServiceError(error: { [key: string]: unknown }): Observable<LayoutServiceError> {
if (error instanceof LayoutServiceError) {
return observableThrow(error);
}
const layoutServiceError = new LayoutServiceError();
const response = error.response as { status: number; statusText: string; data: unknown };
if (response) {
Expand Down