1234567891011121314151617181920212223242526272829303132333435363738394041424344454647 |
- 'use strict'
- import _fs, { promises as fs } from 'fs'
- import boom from 'boom'
- import { imagesPath } from '../config'
- /**
- * Call the error handler if a middleware function throw an error
- *
- * @param {Function} fn original middleware function of the route
- * @returns {Function} the same middleware function of the route but error handled
- */
- export const asyncMiddleware = fn => (req, res, next) => {
- Promise.resolve(fn(req, res, next)).catch(err => {
- // Check whether the error is a boom error
- if (!err.isBoom) {
- // The error was not recognized, send a 500 HTTP error
- return next(boom.internal(err))
- }
- // It is a boom error, pass it to the error handler
- next(err)
- })
- }
- // Middleware to handle middleware errors
- export const errorHandler = (err, req, res, next) => {
- const { output: { payload } } = err
- console.error(`Error ${payload.statusCode} - ${payload.error}\n${payload.message}\n`)
- return res.status(payload.statusCode).json(payload)
- }
- /**
- * Get a list of all available scenes
- *
- * @returns {string[]} the available scenes
- */
- export const getAvailableScenes = async () => {
- try {
- // Check if the images directory exists
- await fs.access(imagesPath, _fs.constants.R_OK)
- }
- catch (err) {
- // The images directory does not exist or is not accessible
- throw boom.badRequest(`Can't access the "${imagesPath}" directory. Check it exists and you have read permission on it.`)
- }
- return fs.readdir(imagesPath)
- }
|