centralise msgs in copy/en.js

plus some other fixes
This commit is contained in:
Lachlan Kermode
2018-12-06 16:22:10 +00:00
parent 4da10b1409
commit 72edac944c
6 changed files with 72 additions and 56 deletions

View File

@@ -21,8 +21,8 @@ export default ({ config, controller }) => {
.retrieveFrag(source, tab, resource, frag) .retrieveFrag(source, tab, resource, frag)
.then(data => res.json(data)) .then(data => res.json(data))
.catch(err => .catch(err =>
res.status(err.status || 501) res.status(err.status || 404)
.send() .send({ error: err.message })
) )
}) })
@@ -32,20 +32,10 @@ export default ({ config, controller }) => {
.then(data => res.json(data)) .then(data => res.json(data))
.catch(err => .catch(err =>
res.status(err.status || 404) res.status(err.status || 404)
.send({ error: err }) .send({ error: err.message })
) )
}) })
api.get('/:source', (req, res) => {
res.status(404)
.send({ error: copy.errors.onlySource })
})
api.get('/:source/:tab', (req, res) => {
res.status(404)
.send({ error: copy.errors.onlyTab })
})
api.get('/update', (req, res) => { api.get('/update', (req, res) => {
controller controller
.update() .update()
@@ -55,11 +45,23 @@ export default ({ config, controller }) => {
}) })
) )
.catch(err => .catch(err =>
res.json({ res.status(404)
error: err.message .send({ error: err.message })
})
) )
}) })
// ERROR routes. Note that it is important that these come AFTER routes
// like /update, so that the regex does not greedily match these routes.
api.get('/:source', (req, res) => {
res.status(404)
.send({ error: copy.errors.onlySource })
})
api.get('/:source/:tab', (req, res) => {
res.status(404)
.send({ error: copy.errors.onlyTab })
})
return api return api
} }

View File

@@ -1,6 +1,13 @@
export default { export default {
errors: { errors: {
update: 'The server could not update. Check your API credentials and internet connection and try again.',
onlySource: 'You cannot query a source directly. The URL needs to be in the format /:source/:tab/:resource.', onlySource: 'You cannot query a source directly. The URL needs to be in the format /:source/:tab/:resource.',
onlyTab: 'You cannot query a tab directly. The URL needs to be in the format /:source/:tab/:resource.' onlyTab: 'You cannot query a tab directly. The URL needs to be in the format /:source/:tab/:resource.',
noSource: source => `The source ${source} is not available in this server.`,
noResource: prts => `The resource '${prts[2]}' does not exists in the tab '${prts[1]}' of the source '${prts[0]}'.`,
noFragment: prts => `Fragment index does not exist`
},
success: {
update: 'All sources updated'
} }
} }

View File

@@ -1,3 +1,5 @@
import copy from '../copy/en'
/** /**
* Controller * Controller
* *
@@ -23,7 +25,11 @@ class Controller {
return this.fetchers[source].update() return this.fetchers[source].update()
}) })
).then(results => { ).then(results => {
return 'All sources updated' if (results.every(r => r)) {
return copy.success.update
} else {
throw new Error(copy.errors.update)
}
}) })
} }
@@ -32,9 +38,7 @@ class Controller {
const fetcher = this.fetchers[source] const fetcher = this.fetchers[source]
return fetcher.retrieve(tab, resource) return fetcher.retrieve(tab, resource)
} else { } else {
return Promise.resolve().then(() => { return Promise.reject(new Error(copy.errors.noResource(source)))
throw new Error(`Source ${source} not available.`)
})
} }
} }
@@ -43,9 +47,7 @@ class Controller {
const fetcher = this.fetchers[source] const fetcher = this.fetchers[source]
return fetcher.retrieveFrag(tab, resource, frag) return fetcher.retrieveFrag(tab, resource, frag)
} else { } else {
return Promise.resolve().then(() => { return Promise.reject(new Error(copy.errors.noResource(source)))
throw new Error(`Source ${source} not available.`)
})
} }
} }
} }

View File

@@ -111,7 +111,8 @@ class Fetcher {
}) })
) )
}) })
.then(() => 'All tabs updated') .then(() => true)
.catch(() => false)
} }
save (tab, data) { save (tab, data) {

View File

@@ -2,6 +2,12 @@
/** /**
* Model is a class whose sole responsibility is to save and load blueprints. * Model is a class whose sole responsibility is to save and load blueprints.
* It allows for different storage mechanisms for different kinds of blueprints. * It allows for different storage mechanisms for different kinds of blueprints.
*
* ERRORS:
* When a load function fails, it should throw either:
* 1. A __ error if the resource doesn't exist on that sheet/tab.
* 2. A __ error if a fragment lookup fails because it doesn't exist.
* 3. A __ error if something else goes wrong.
*/ */
class Model { class Model {
/** /**

View File

@@ -1,5 +1,6 @@
import fs from 'mz/fs' import fs from 'mz/fs'
import { fmtSourceTitle } from '../lib/util' import { fmtSourceTitle } from '../lib/util'
import copy from '../copy/en'
const STORAGE_DIRNAME = 'temp' const STORAGE_DIRNAME = 'temp'
@@ -22,39 +23,36 @@ class StoreJson {
const fname = `${STORAGE_DIRNAME}/${parts[0]}__${parts[1]}__${ const fname = `${STORAGE_DIRNAME}/${parts[0]}__${parts[1]}__${
parts[2] parts[2]
}.json` }.json`
return fs if (fs.existsSync(fname)) {
.exists(fname) return fs.readFile(fname, 'utf8')
.then(isAvailable => { .then(data => JSON.parse(data))
if (isAvailable) return fs.readFile(fname, 'utf8') .then(data => {
else { if (parts.length === 3) {
throw new Error('No resource exists') // No lookup if the requested url doesn't have a fragment
} return data
}) } else if (parts[2] === 'ids') {
.then(data => JSON.parse(data)) // Do a lookup if fragment is included to filter a relevant item
.then(data => { // When the resource requested is 'ids'
if (parts.length === 3) { const id = parseInt(parts[3])
// No lookup if the requested url doesn't have a fragment if (!isNaN(id) && id >= 0 && id < data.length) {
return data return data[id]
} else if (parts[2] === 'ids') { } else {
// Do a lookup if fragment is included to filter a relevant item throw new Error(copy.errors.noFragment(parts))
// When the resource requested is 'ids' }
const id = parseInt(parts[3])
if (!isNaN(id) && id >= 0 && id < data.length) {
return data[id]
} else { } else {
throw new Error(`Fragment index does not exist`) // Do a lookup if fragment is included to filter a relevant item
const index = parseInt(parts[3])
if (!isNaN(index) && index >= 0 && index < data.length) {
console.log(data, index)
return data.filter((vl, idx) => idx === index)[0]
} else {
throw new Error(copy.errors.noFragment(parts))
}
} }
} else { })
// Do a lookup if fragment is included to filter a relevant item } else {
const index = parseInt(parts[3]) return Promise.reject(new Error(copy.errors.noResource(parts)))
if (!isNaN(index) && index >= 0 && index < data.length) { }
console.log(data, index)
return data.filter((vl, idx) => idx === index)[0]
} else {
throw new Error(`Fragment index does not exist`)
}
}
})
} }
// TODO: add method to build blueprint from data source // TODO: add method to build blueprint from data source