summaryrefslogtreecommitdiffstats
path: root/lib/utils
diff options
context:
space:
mode:
authorSamy Pessé <samypesse@gmail.com>2016-02-11 10:07:15 +0100
committerSamy Pessé <samypesse@gmail.com>2016-02-11 10:07:15 +0100
commit52c4dc49cf460dad4edc42457ba9e3ca4c7674d5 (patch)
tree6d5d123756c8d79ab5add4660e7b66beb3e89736 /lib/utils
parent44ad70320a0d0b907702c57e560c6a60373c9bca (diff)
downloadgitbook-52c4dc49cf460dad4edc42457ba9e3ca4c7674d5.zip
gitbook-52c4dc49cf460dad4edc42457ba9e3ca4c7674d5.tar.gz
gitbook-52c4dc49cf460dad4edc42457ba9e3ca4c7674d5.tar.bz2
Normalize errors
Diffstat (limited to 'lib/utils')
-rw-r--r--lib/utils/error.js53
-rw-r--r--lib/utils/path.js11
2 files changed, 59 insertions, 5 deletions
diff --git a/lib/utils/error.js b/lib/utils/error.js
index 66e20db..4ccf83d 100644
--- a/lib/utils/error.js
+++ b/lib/utils/error.js
@@ -1,4 +1,6 @@
var _ = require('lodash');
+var TypedError = require('error/typed');
+var WrappedError = require('error/wrapped');
// Enforce as an Error object, and cleanup message
function enforce(err) {
@@ -8,6 +10,55 @@ function enforce(err) {
return err;
}
+// Random error wrappers during parsing/generation
+var ParsingError = WrappedError({
+ message: 'Parsing Error: {origMessage}',
+ type: 'server.parsing-failed'
+});
+var GenerationError = WrappedError({
+ message: 'Generation Error: {origMessage}',
+ type: 'server.parsing-failed'
+});
+
+// Error when output generator does not exists
+var GeneratorNotFoundError = TypedError({
+ type: 'server.404',
+ message: 'Generator "{generator}" does not exists',
+ generator: null
+});
+
+// A file does not exists
+var FileNotFoundError = TypedError({
+ type: 'server.404',
+ message: 'No "{filename}" file (or is ignored)',
+ filename: null
+});
+
+// A file is outside the scope
+var FileOutOfScopeError = TypedError({
+ type: 'server.404',
+ message: '"{filename}" not in "{root}"',
+ filename: null,
+ root: null,
+ code: 'EACCESS'
+});
+
+// Error for nunjucks templates
+var TemplateError = WrappedError({
+ message: 'Error compiling template "{filename}": {origMessage}',
+ type: 'client.template-failed',
+ filename: null
+});
+
module.exports = {
- enforce: enforce
+ enforce: enforce,
+
+ ParsingError: ParsingError,
+ GenerationError: GenerationError,
+
+ FileNotFoundError: FileNotFoundError,
+ FileOutOfScopeError: FileOutOfScopeError,
+
+ GeneratorNotFoundError: GeneratorNotFoundError,
+ TemplateError: TemplateError
};
diff --git a/lib/utils/path.js b/lib/utils/path.js
index 2d722a5..f195d6c 100644
--- a/lib/utils/path.js
+++ b/lib/utils/path.js
@@ -1,6 +1,8 @@
var _ = require('lodash');
var path = require('path');
+var error = require('./error');
+
// Normalize a filename
function normalizePath(filename) {
return path.normalize(filename);
@@ -15,7 +17,7 @@ function isInRoot(root, filename) {
// Resolve paths in a specific folder
// Throw error if file is outside this folder
function resolveInRoot(root) {
- var input, result, err;
+ var input, result;
input = _.chain(arguments)
.toArray()
@@ -31,9 +33,10 @@ function resolveInRoot(root) {
result = path.resolve(root, input);
if (!isInRoot(root, result)) {
- err = new Error('EACCESS: "' + result + '" not in "' + root + '"');
- err.code = 'EACCESS';
- throw err;
+ throw new error.FileOutOfScopeError({
+ filename: result,
+ root: root
+ });
}
return result;