add http error class and save file correctly
parent
d0d5338df5
commit
95788cb8e4
|
@ -1,3 +1,4 @@
|
||||||
|
'use strict'
|
||||||
/**
|
/**
|
||||||
* BooksController
|
* BooksController
|
||||||
*
|
*
|
||||||
|
@ -5,6 +6,8 @@
|
||||||
* @help :: See https://sailsjs.com/docs/concepts/actions
|
* @help :: See https://sailsjs.com/docs/concepts/actions
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
const HttpError = require('../errors/HttpError')
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
publish: async function (req, res) {
|
publish: async function (req, res) {
|
||||||
try {
|
try {
|
||||||
|
@ -18,9 +21,9 @@ module.exports = {
|
||||||
const host = req.hostname
|
const host = req.hostname
|
||||||
let result
|
let result
|
||||||
|
|
||||||
if (!host) throw new Error('Missing hostname')
|
if (!host) throw new HttpError(400, 'Missing hostname')
|
||||||
if (!body) throw new Error('Missing body')
|
if (!body) throw new HttpError(400, 'Missing body')
|
||||||
if (!body.title || !body.author || !body.version || !body.opds) throw new Error('Body is not formatted correctly')
|
if (!body.title || !body.author || !body.version || !req.file('opds')) throw new Error('Body is not formatted correctly')
|
||||||
const data = {
|
const data = {
|
||||||
source: host,
|
source: host,
|
||||||
title: body.title,
|
title: body.title,
|
||||||
|
@ -31,22 +34,24 @@ module.exports = {
|
||||||
const bookExists = await Book.findOne(data)
|
const bookExists = await Book.findOne(data)
|
||||||
|
|
||||||
if (bookExists) {
|
if (bookExists) {
|
||||||
throw new Error('Version already exists')
|
throw new HttpError(400, 'Version already exists')
|
||||||
} else {
|
} else {
|
||||||
result = await Book.create({
|
result = await Book.create(data).fetch()
|
||||||
...data,
|
|
||||||
publishDate: (new Date()).toISOString()
|
|
||||||
}).fetch()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
req.file('opds').upload(sails.config.skipperConfig, function (err, uploaded) {
|
req.file('opds').upload(sails.config.skipperConfig, async function (err, uploaded) {
|
||||||
if (err) throw err
|
if (err) {
|
||||||
console.log(uploaded)
|
await result.destroy()
|
||||||
|
throw new HttpError(500, err.message)
|
||||||
|
}
|
||||||
|
result.storage = uploaded.fd
|
||||||
|
await result.save()
|
||||||
return res.json({
|
return res.json({
|
||||||
...result
|
...result
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
|
if (e instanceof HttpError) return e.send(res)
|
||||||
return res.status(400).json({
|
return res.status(400).json({
|
||||||
error: e.message
|
error: e.message
|
||||||
})
|
})
|
||||||
|
@ -56,17 +61,15 @@ module.exports = {
|
||||||
list: async function (req, res) {
|
list: async function (req, res) {
|
||||||
try {
|
try {
|
||||||
const body = req.allParams()
|
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)
|
const books = await Book.find(body)
|
||||||
|
|
||||||
if (!books.length) {
|
if (!books.length) throw new HttpError(404, 'No books matching those parameters were found.')
|
||||||
return res.status(404).json({
|
|
||||||
error: 'No books matching those parameters were found.'
|
|
||||||
})
|
|
||||||
}
|
|
||||||
return res.json(books)
|
return res.json(books)
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
|
if (e instanceof HttpError) return e.send(res)
|
||||||
return res.status(500).json({
|
return res.status(500).json({
|
||||||
error: e.message
|
error: e.message
|
||||||
})
|
})
|
||||||
|
|
|
@ -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
|
|
@ -18,11 +18,11 @@ module.exports = {
|
||||||
autoIncrement: true
|
autoIncrement: true
|
||||||
},
|
},
|
||||||
source: { type: 'string' },
|
source: { type: 'string' },
|
||||||
|
storage: { type: 'string' },
|
||||||
title: { type: 'string', required: true },
|
title: { type: 'string', required: true },
|
||||||
author: { type: 'string' },
|
author: { type: 'string' },
|
||||||
version: { type: 'string' },
|
version: { type: 'string' },
|
||||||
isbn: { type: 'string' },
|
isbn: { type: 'string' }
|
||||||
publishDate: { type: 'string' }
|
|
||||||
|
|
||||||
// ╔═╗╔╦╗╔╗ ╔═╗╔╦╗╔═╗
|
// ╔═╗╔╦╗╔╗ ╔═╗╔╦╗╔═╗
|
||||||
// ║╣ ║║║╠╩╗║╣ ║║╚═╗
|
// ║╣ ║║║╠╩╗║╣ ║║╚═╗
|
||||||
|
|
Loading…
Reference in New Issue