const sendErrorResponse = res => { res.writeHead(httpStatus.NOT_FOUND, { "Content-Type": "text/html" }); res.write("<h1>File Not Found!</h1>"); res.end(); }
// Check if the route is not found or HTTP method is not supported app.use('*', (req, res) => { if (routes[req.baseUrl]) { res.status(HttpStatus.METHOD_NOT_ALLOWED).json({ message: 'The requested HTTP method is not supported.' }) } else { res.status(HttpStatus.NOT_FOUND).json({ message: 'The requested resource cannot be found.' }) } })
app.use('*', (req, res) => { res.status(HttpStatus.NOT_FOUND).send('404 Not Found') })
/** * Ensure project exist * @param {String} projectId the project id * @param {String} userToken the user token */ async function ensureProjectExist (projectId, userToken) { let token = await getM2MToken() const url = `${config.PROJECTS_API_URL}/${projectId}` try { await axios.get(url, { headers: { Authorization: `Bearer ${token}` } }) } catch (err) { if (_.get(err, 'response.status') === HttpStatus.NOT_FOUND) { throw new errors.BadRequestError(`Project with id: ${projectId} doesn't exist`) } else { // re-throw other error throw err } } }
/** * This function gets the challenge terms array with the terms data * The terms data is retrieved from the terms API using the specified terms ids * * @param {Array<Object>} terms The array of terms {id, roleId} to retrieve from terms API */ async function validateChallengeTerms (terms = []) { const listOfTerms = [] const token = await getM2MToken() for (let term of terms) { // Get the terms details from the API try { await axios.get(`${config.TERMS_API_URL}/${term.id}`, { headers: { Authorization: `Bearer ${token}` } }) listOfTerms.push(term) } catch (e) { if (_.get(e, 'response.status') === HttpStatus.NOT_FOUND) { throw new errors.BadRequestError(`Terms of use identified by the id ${term.id} does not exist`) } else { // re-throw other error throw e } } } return listOfTerms }
HttpStatus.NOT_FOUND, 'User not found' );
}) } catch (e) { if (e.statusCode === HttpStatus.NOT_FOUND) { throw new errors.NotFoundError(`Challenge of id ${id} is not found.`) } else {
app.use('*', (req, res) => { res.status(HttpStatus.NOT_FOUND).send('404 Not Found') })