forked from pencilblue/pencilblue
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request pencilblue#1185 from pencilblue/feature/970
Fixes pencilblue#970 - Improved Routing Capabilities
- Loading branch information
Showing
32 changed files
with
3,022 additions
and
360 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
/* | ||
Copyright (C) 2016 PencilBlue, LLC | ||
This program is free software: you can redistribute it and/or modify | ||
it under the terms of the GNU General Public License as published by | ||
the Free Software Foundation, either version 3 of the License, or | ||
(at your option) any later version. | ||
This program is distributed in the hope that it will be useful, | ||
but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
GNU General Public License for more details. | ||
You should have received a copy of the GNU General Public License | ||
along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
'use strict'; | ||
|
||
//dependencies | ||
var HttpStatusCodes = require('http-status-codes'); | ||
|
||
/** | ||
* Provides convenience functions to create errors for specific conditions | ||
* @class ErrorUtils | ||
*/ | ||
class ErrorUtils { | ||
|
||
/** | ||
* Creates an error that represents when a resource is not found (404) | ||
* @static | ||
* @method notFound | ||
* @param {string} [message] | ||
* @returns {Error} | ||
*/ | ||
static notFound (message) { | ||
return ErrorUtils.custom(message, HttpStatusCodes.NOT_FOUND); | ||
} | ||
|
||
/** | ||
* Creates an error that represents a lack of permission (403) | ||
* @static | ||
* @method forbidden | ||
* @param {string} [message] | ||
* @returns {Error} | ||
*/ | ||
static forbidden (message) { | ||
return ErrorUtils.custom(message, HttpStatusCodes.FORBIDDEN); | ||
} | ||
|
||
/** | ||
* Creates an error that represents an unauthorized request (401) | ||
* @static | ||
* @method notAuthorized | ||
* @param {string} [message] | ||
* @returns {Error} | ||
*/ | ||
static notAuthorized (message) { | ||
return ErrorUtils.custom(message, HttpStatusCodes.UNAUTHORIZED); | ||
} | ||
|
||
/** | ||
* Creates an error that represents an internal server error | ||
* @static | ||
* @method badRequest | ||
* @param {object} [options] | ||
* @param {string} [options.message] | ||
* @param {Array} [options.validationErrors] | ||
* @returns {Error} | ||
*/ | ||
static badRequest (options) { | ||
options = options || {}; | ||
var err = ErrorUtils.custom(options.message, HttpStatusCodes.BAD_REQUEST); | ||
err.validationErrors = options.validationErrors; | ||
return err; | ||
} | ||
|
||
/** | ||
* Creates an error that represents an internal server error (500) | ||
* @static | ||
* @method internalServerError | ||
* @param {string} [message] | ||
* @returns {Error} | ||
*/ | ||
static internalServerError (message) { | ||
return ErrorUtils.custom(message); | ||
} | ||
|
||
/** | ||
* Creates a custom error with a specific message and status code | ||
* @param {string} [message='An Error Occurred'] | ||
* @param {Number} [code=500] | ||
* @return {Error} | ||
*/ | ||
static custom (message, code) { | ||
code = code || HttpStatusCodes.INTERNAL_SERVER_ERROR; | ||
var err = new Error(message || HttpStatusCodes.getStatusText(code) || 'An Error Occurred'); | ||
err.code = code; | ||
return err; | ||
} | ||
} | ||
|
||
module.exports = ErrorUtils; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
/* | ||
Copyright (C) 2016 PencilBlue, LLC | ||
This program is free software: you can redistribute it and/or modify | ||
it under the terms of the GNU General Public License as published by | ||
the Free Software Foundation, either version 3 of the License, or | ||
(at your option) any later version. | ||
This program is distributed in the hope that it will be useful, | ||
but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
GNU General Public License for more details. | ||
You should have received a copy of the GNU General Public License | ||
along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
'use strict'; | ||
|
||
//dependencies | ||
var HtmlEncoder = require('htmlencode'); | ||
|
||
/** | ||
* Responsible for formatting an error as XML | ||
* @class XmlErrorFormatter | ||
*/ | ||
class XmlErrorFormatter { | ||
|
||
/** | ||
* Serializes the object version of the error to be formatted | ||
* @method serialize | ||
* @param {object} params | ||
* @returns {string} | ||
*/ | ||
static serialize(params) { | ||
return XmlErrorFormatter.serializeXmlObject('error', params); | ||
} | ||
|
||
/** | ||
* @static | ||
* @method serializeXmlPrimitive | ||
* @param {string} key | ||
* @param {*} val | ||
* @returns {string} | ||
*/ | ||
static serializeXmlPrimitive (key, val) { | ||
return XmlErrorFormatter.startElement(key) + HtmlEncoder.htmlEncode(val + '') + XmlErrorFormatter.endElement(key); | ||
} | ||
|
||
/** | ||
* @static | ||
* @method serializeXmlArray | ||
* @param {string} key | ||
* @param {Array} obj | ||
* @returns {string} | ||
*/ | ||
static serializeXmlArray (key, obj) { | ||
var xml = ''; | ||
obj.forEach(function(prop, i) { | ||
var val = obj[prop]; | ||
if (Array.isArray(val)) { | ||
xml += XmlErrorFormatter.serializeXmlArray(key + '_' + i, val); | ||
} | ||
else if (typeof val === 'object') { | ||
xml += XmlErrorFormatter.serializeXmlObject(key, val); | ||
} | ||
else { | ||
xml += XmlErrorFormatter.serializeXmlPrimitive(key, val); | ||
} | ||
}); | ||
return xml; | ||
} | ||
|
||
/** | ||
* @static | ||
* @method serializeXmlObject | ||
* @param {string} key | ||
* @param {object} obj | ||
* @returns {string} | ||
*/ | ||
static serializeXmlObject (key, obj) { | ||
var xml = XmlErrorFormatter.startElement(key); | ||
Object.keys(obj).forEach(function(val) { | ||
if (Array.isArray(val)) { | ||
xml += XmlErrorFormatter.serializeXmlArray(key, val); | ||
} | ||
else if (typeof val === 'object') { | ||
xml += XmlErrorFormatter.serializeXmlObject(key, val); | ||
} | ||
else { | ||
xml += XmlErrorFormatter.serializeXmlPrimitive(key, val); | ||
} | ||
}); | ||
xml += XmlErrorFormatter.endElement(key); | ||
return xml; | ||
} | ||
|
||
/** | ||
* @static | ||
* @method startElement | ||
* @param {string} key | ||
* @returns {string} | ||
*/ | ||
static startElement (key) { | ||
return '<' + HtmlEncoder.htmlEncode(key) + '>'; | ||
} | ||
|
||
/** | ||
* @static | ||
* @method endElement | ||
* @param {string} key | ||
* @returns {string} | ||
*/ | ||
static endElement (key) { | ||
return '</' + HtmlEncoder.htmlEncode(key) + '>'; | ||
} | ||
} | ||
|
||
module.exports = XmlErrorFormatter; |
Oops, something went wrong.