From ad9c9fd678dbab49c268b7b16971b44e04e7217c Mon Sep 17 00:00:00 2001 From: Ferdinand Kuhl Date: Tue, 8 Aug 2023 10:48:14 +0200 Subject: [PATCH] Adding error handling to code example --- README.md | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 0e01ccb..0c0294d 100644 --- a/README.md +++ b/README.md @@ -34,11 +34,14 @@ Your JavaScript could look like that: ```javascript async function translate(idPatterns) { - return fetch(document.location, {headers: { + const response = await fetch(document.location, {headers: { 'X-Translation-Request': idPatterns, 'Accept-Language': document.documentElement.lang - }}) - .then(response => response.json()); + }}); + if (! response.ok) { + return Promise.reject('Unexpected server response'); + } + return await response.json(); } ```