Skip to content

Commit

Permalink
fix: Handle erroneous status codes and messages in Brazil time fetch
Browse files Browse the repository at this point in the history
  • Loading branch information
drikusroor committed Oct 7, 2024
1 parent afef3c3 commit d63a07c
Showing 1 changed file with 13 additions and 0 deletions.
13 changes: 13 additions & 0 deletions src/blog/templates/blog/includes/brazil-time.html
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,19 @@
function updateBrazilTime() {
fetch('/blog/api/brazil_time/')
.then(response => response.text())
.then((text) => {
// if text contains an erroneous status code or message, throw an error

// status code regex, find the status code in the text that starts with 3, 4, or 5 and is followed by 2 digits
const statusCodeRegex = /(?:3|4|5)\d{2}/;

// find "Bad Request" or "Internal Server Error" or "Bad Gateway" in the text
const statusMessageRegex = /Bad Request|Internal Server Error|Bad Gateway/;

if (statusCodeRegex.test(text) || statusMessageRegex.test(text)) {
throw new Error(text);
}
})
.then(time => {
document.getElementById('brazilTime').innerHTML = time;
})
Expand Down

0 comments on commit d63a07c

Please sign in to comment.