add http error class and save file correctly

pull/23/head
unknown 2018-11-12 16:20:27 -05:00
parent d0d5338df5
commit 95788cb8e4
3 changed files with 38 additions and 19 deletions

View File

@ -1,3 +1,4 @@
'use strict'
/**
* BooksController
*
@ -5,6 +6,8 @@
* @help :: See https://sailsjs.com/docs/concepts/actions
*/
const HttpError = require('../errors/HttpError')
module.exports = {
publish: async function (req, res) {
try {
@ -18,9 +21,9 @@ module.exports = {
const host = req.hostname
let result
if (!host) throw new Error('Missing hostname')
if (!body) throw new Error('Missing body')
if (!body.title || !body.author || !body.version || !body.opds) throw new Error('Body is not formatted correctly')
if (!host) throw new HttpError(400, 'Missing hostname')
if (!body) throw new HttpError(400, 'Missing body')
if (!body.title || !body.author || !body.version || !req.file('opds')) throw new Error('Body is not formatted correctly')
const data = {
source: host,
title: body.title,
@ -31,22 +34,24 @@ module.exports = {
const bookExists = await Book.findOne(data)
if (bookExists) {
throw new Error('Version already exists')
throw new HttpError(400, 'Version already exists')
} else {
result = await Book.create({
...data,
publishDate: (new Date()).toISOString()
}).fetch()
result = await Book.create(data).fetch()
}
req.file('opds').upload(sails.config.skipperConfig, function (err, uploaded) {
if (err) throw err
console.log(uploaded)
req.file('opds').upload(sails.config.skipperConfig, async function (err, uploaded) {
if (err) {
await result.destroy()
throw new HttpError(500, err.message)
}
result.storage = uploaded.fd
await result.save()
return res.json({
...result
})
})
} catch (e) {
if (e instanceof HttpError) return e.send(res)
return res.status(400).json({
error: e.message
})
@ -56,17 +61,15 @@ module.exports = {
list: async function (req, res) {
try {
const body = req.allParams()
if (!body) { throw new Error('Missing parameters') }
if (!body) { throw new HttpError(400, 'Missing parameters') }
const books = await Book.find(body)
if (!books.length) {
return res.status(404).json({
error: 'No books matching those parameters were found.'
})
}
if (!books.length) throw new HttpError(404, 'No books matching those parameters were found.')
return res.json(books)
} catch (e) {
if (e instanceof HttpError) return e.send(res)
return res.status(500).json({
error: e.message
})

16
api/errors/HttpError.js Normal file
View File

@ -0,0 +1,16 @@
class HttpError extends Error {
constructor (status, message, hint) {
super(message)
if (typeof status !== 'number') throw new Error('HttpError status must be an integer')
this.status = status
this.hint = hint || 'none'
}
send (res) {
return res.status(this.status).json({
error: this.message,
hint: this.hint
})
}
}
module.exports = HttpError

View File

@ -18,11 +18,11 @@ module.exports = {
autoIncrement: true
},
source: { type: 'string' },
storage: { type: 'string' },
title: { type: 'string', required: true },
author: { type: 'string' },
version: { type: 'string' },
isbn: { type: 'string' },
publishDate: { type: 'string' }
isbn: { type: 'string' }
// ╔═╗╔╦╗╔╗ ╔═╗╔╦╗╔═╗
// ║╣ ║║║╠╩╗║╣ ║║╚═╗