Skip to content

Commit aa01f74

Browse files
authored
Merge pull request #182 from episerver/feature/CMS-47081-errors
Runtime errors
2 parents 09e7394 + 1ad9c43 commit aa01f74

File tree

3 files changed

+41
-6
lines changed

3 files changed

+41
-6
lines changed

packages/optimizely-cms-sdk/src/graph/error.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ type GraphRequest = {
1313
export class OptimizelyGraphError extends Error {
1414
constructor(message: string) {
1515
super(message);
16+
this.name = 'OptimizelyGraphError';
1617
}
1718
}
1819

@@ -29,6 +30,7 @@ export class GraphMissingContentTypeError extends OptimizelyGraphError {
2930
super(
3031
`Content type "${contentType}" not included in the registry. Ensure that you called "initContentTypeRegistry()" with it before fetching content.`
3132
);
33+
this.name = 'GraphMissingContentTypeError';
3234
this.contentType = contentType;
3335
}
3436
}
@@ -39,6 +41,7 @@ export class GraphResponseError extends OptimizelyGraphError {
3941
constructor(message: string, options: { request: GraphRequest }) {
4042
super(message);
4143
this.request = options.request;
44+
this.name = 'GraphResponseError';
4245
}
4346
}
4447

@@ -52,6 +55,7 @@ export class GraphHttpResponseError extends GraphResponseError {
5255
) {
5356
super(message, options);
5457
this.status = options.status;
58+
this.name = 'GraphHttpResponseError';
5559
}
5660
}
5761

@@ -81,5 +85,6 @@ export class GraphContentResponseError extends GraphHttpResponseError {
8185
super(message, options);
8286

8387
this.errors = errors;
88+
this.name = 'GraphContentResponseError';
8489
}
8590
}

packages/optimizely-cms-sdk/src/react/server.tsx

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -105,12 +105,6 @@ export async function OptimizelyComponent({
105105
});
106106

107107
if (!Component) {
108-
console.log(
109-
`[optimizely-cms-sdk] No component found for content type ${
110-
opti.__typename
111-
} ${opti.__tag ? `with tag "${opti.__tag}"` : ''}`
112-
);
113-
114108
return (
115109
<FallbackComponent>
116110
No component found for content type <b>{opti.__typename}</b>
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
'use client';
2+
3+
import { useEffect } from 'react';
4+
import { GraphErrors } from '@optimizely/cms-sdk';
5+
6+
export default function Error({
7+
error,
8+
reset,
9+
}: {
10+
error: Error & { digest?: string };
11+
reset: () => void;
12+
}) {
13+
useEffect(() => {
14+
// Use `instanceof` to get specific types of errors and handle differently
15+
if (error instanceof GraphErrors.GraphResponseError) {
16+
console.error(
17+
'Optimizely Graph Error: ',
18+
error.message,
19+
'see the query and variables in the logs'
20+
);
21+
console.log('Showing the query:');
22+
console.log(error.request.query);
23+
console.log('Showing variables:');
24+
console.log(error.request.variables);
25+
} else {
26+
console.error(error);
27+
}
28+
}, [error]);
29+
30+
return (
31+
<div>
32+
<h2>Something went wrong!</h2>
33+
<button onClick={() => reset()}>Try again</button>
34+
</div>
35+
);
36+
}

0 commit comments

Comments
 (0)