Skip to content

Commit

Permalink
Merge pull request #202 from mhsdesign/feature/betterExceptionHandling3
Browse files Browse the repository at this point in the history
Feature: better exception handling
  • Loading branch information
mficzel authored Oct 27, 2022
2 parents 94526b1 + b718106 commit 7c9e12d
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
.message {
text-align: center;
width: 100%;
white-space: pre-line;
}
}

Expand Down
35 changes: 34 additions & 1 deletion Resources/Private/JavaScript/state/business/sagas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,39 @@ export function* operation(task: () => SagaIterator<void>) {
yield put(actions.finishTask(taskName));
}

const throwErrorWithMessageFromFailedHttpResponse = async (response: Response) => {
const message = await response.text();

if (message.includes('Flow-Debug-Exception-Header') === false
&& message.includes('neos-error-screen') === false) {
throw new Error(`Network response was not ok: (${response.status})\n${response.statusText}`);
}

const htmlContainer = document.createElement('div');
htmlContainer.innerHTML = message;

// similar how the neos-ui "hacks" it
// https://github.com/neos/neos-ui/blob/e53f8e20ee70ca832828f033f5a69c8223a2deab/packages/neos-ui/src/index.js#L161-L191
const exceptionHeader = htmlContainer.querySelector('.Flow-Debug-Exception-Header');
if (exceptionHeader && exceptionHeader.textContent?.trim().length) {
const exceptionSubject = exceptionHeader.querySelector('.ExceptionSubject')!;
const exceptionBody = exceptionHeader.querySelector('.ExceptionBody')!;
throw new Error(`Network response was not ok: (${response.status})\n\n${exceptionSubject.textContent}\n${exceptionBody.textContent}`);
}

const neosErrorScreen = htmlContainer.querySelector('.neos-error-screen');
if (neosErrorScreen && neosErrorScreen.textContent?.trim().length) {
const neos8FusionSyntaxErrorHeading = neosErrorScreen.querySelector("h3")?.textContent
throw new Error(`Network response was not ok: (${response.status})\n\n${
neos8FusionSyntaxErrorHeading?.length
? neos8FusionSyntaxErrorHeading
: neosErrorScreen.textContent
}`);
}

throw new Error(`Network response was not ok: (${response.status})\nUnknown error from unexpected HTML response.`);
}

export async function unauthenticated(url: string, options?: RequestInit) {
const response = await fetch(url, { ...options, credentials: 'include' });

Expand All @@ -32,7 +65,7 @@ export async function unauthenticated(url: string, options?: RequestInit) {
return 'RE-AUTHORIZE';
}

throw new Error(`Network response was not ok: (${response.status}) ${response.statusText}`);
await throwErrorWithMessageFromFailedHttpResponse(response)
}

export function authenticated(url: string, options?: RequestInit) {
Expand Down
2 changes: 1 addition & 1 deletion Resources/Public/JavaScript/App.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion Resources/Public/JavaScript/App.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion Resources/Public/Styles/App.css

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Resources/Public/Styles/App.css.map

Large diffs are not rendered by default.

0 comments on commit 7c9e12d

Please sign in to comment.