Skip to content

Commit

Permalink
Cleaner way to create error response
Browse files Browse the repository at this point in the history
  • Loading branch information
dpilafian committed Jan 27, 2024
1 parent 3434c0f commit dade5ad
Showing 1 changed file with 4 additions and 5 deletions.
9 changes: 4 additions & 5 deletions w3c-html-validator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 ?? <ReasonError['response']>{};
errRes.body = { messages: [{ type: 'network-error', message: message.join(' ') }] };
const errRes = reason.response ?? <ReasonError['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);
Expand Down

0 comments on commit dade5ad

Please sign in to comment.