From d2dfb4c8df019a066ecd591750762fa07606f51e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?K=C3=A9vin=20Dunglas?= Date: Mon, 25 Jan 2021 02:20:50 +0100 Subject: [PATCH] fix: status code detection (#1440) --- helpers/findStatusGroup.js | 71 +++++++++++++++++++------------------- 1 file changed, 36 insertions(+), 35 deletions(-) diff --git a/helpers/findStatusGroup.js b/helpers/findStatusGroup.js index fef9a6856..ef3baa3f2 100644 --- a/helpers/findStatusGroup.js +++ b/helpers/findStatusGroup.js @@ -1,36 +1,37 @@ -const statusCategories = [ - { - name: "informational", - statusCodeRegex: new RegExp(/[1][0-9]+/), - className: "info-response", - }, - { - name: "successful", - statusCodeRegex: new RegExp(/[2][0-9]+/), - className: "success-response", - }, - { - name: "redirection", - statusCodeRegex: new RegExp(/[3][0-9]+/), - className: "redir-response", - }, - { - name: "client error", - statusCodeRegex: new RegExp(/[4][0-9]+/), - className: "cl-error-response", - }, - { - name: "server error", - statusCodeRegex: new RegExp(/[5][0-9]+/), - className: "sv-error-response", - }, - { - // this object is a catch-all for when no other objects match and should always be last - name: "unknown", - statusCodeRegex: new RegExp(/.*/), - className: "missing-data-response", - }, -] +export default function (responseStatus) { + if (responseStatus >= 100 && responseStatus < 200) + return { + name: "informational", + className: "info-response", + } -export default (responseStatus) => - statusCategories.find(({ statusCodeRegex }) => statusCodeRegex.test(responseStatus)) + if (responseStatus >= 200 && responseStatus < 300) + return { + name: "successful", + className: "success-response", + } + + if (responseStatus >= 300 && responseStatus < 400) + return { + name: "redirection", + className: "redir-response", + } + + if (responseStatus >= 400 && responseStatus < 500) + return { + name: "client error", + className: "cl-error-response", + } + + if (responseStatus >= 500 && responseStatus < 600) + return { + name: "server error", + className: "sv-error-response", + } + + // this object is a catch-all for when no other objects match and should always be last + return { + name: "unknown", + className: "missing-data-response", + } +}