From dade5adae2bee19ef040153079769dcb589771c7 Mon Sep 17 00:00:00 2001 From: dpilafian Date: Sat, 27 Jan 2024 03:37:51 -0800 Subject: [PATCH] Cleaner way to create error response --- w3c-html-validator.ts | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/w3c-html-validator.ts b/w3c-html-validator.ts index 76b0689..f4ac508 100644 --- a/w3c-html-validator.ts +++ b/w3c-html-validator.ts @@ -123,11 +123,10 @@ const w3cHtmlValidator = { type ReasonResponse = { request: { url: string }, res: { statusMessage: string }}; type ReasonError = Error & { errno: number, response: request.Response & ReasonResponse }; const handleError = (reason: ReasonError): ValidatorResults => { - const response = reason.response; - const getMsg = () => [response.status, response.res.statusMessage, response.request.url]; - const message = response ? getMsg() : [reason.errno, reason.message]; - const errRes = response ?? {}; - errRes.body = { messages: [{ type: 'network-error', message: message.join(' ') }] }; + const errRes = reason.response ?? {}; + const getMsg = () => [errRes.status, errRes.res.statusMessage, errRes.request.url]; + const message = reason.response ? getMsg() : [reason.errno, reason.message]; + errRes.body = { messages: [{ type: 'network-error', message: message.join(' ') }] }; return toValidatorResults(errRes); }; return w3cRequest.then(filterMessages).then(toValidatorResults).catch(handleError);