Custom HttpError
JavaScript
Medium
3 views
Problem Description
One integer status code is given. If status is 400 or more, throw HttpError with message BAD_REQUEST for 400-499 and SERVER_ERROR for 500+. Catch and print: status message. If
Input Format
One integer status.
Output Format
One line output.
Official Solution
const fs=require('fs');const s=fs.readFileSync(0,'utf8').trim();if(!s)process.exit(0);const status=Number(s);class HttpError extends Error{constructor(status,msg){super(msg);this.status=status;}}try{if(status>=400){const msg=status<500?'BAD_REQUEST':'SERVER_ERROR';throw new HttpError(status,msg);}process.stdout.write('OK');}catch(e){process.stdout.write(String(e.status||status)+' '+String(e.message||'ERR'));}
Solutions (0)
No solutions submitted yet. Be the first!
No comments yet. Start the discussion!