cray
Version:
Epub parser
117 lines (98 loc) • 4.31 kB
JavaScript
/**
* @author: Akshay Kr Singh.
* @date: 20/11/15.
* @github: https://github.com/akshayKrSingh
*/
// Require our core node modules.
var util = require( "util" );
/*// Export the constructor function.
exports.EpubError = EpubError;
// Export the factory function for the custom error object. The factory function lets
// the calling context create new EpubError instances without calling the [new] keyword.
exports.createEpubError = createEpubError;*/
// ----------------------------------------------------------------------------------- //
// ----------------------------------------------------------------------------------- //
// I create the new instance of the EpubError object, ensuring that it properly
// extends from the Error class.
function createEpubError( settings ) {
// NOTE: We are overriding the "implementationContext" so that the createEpubError()
// function is not part of the resulting stacktrace.
return( new EpubError( settings, createEpubError ) );
}
// I am the custom error object for the application. The settings is a hash of optional
// properties for the error instance:
// --
// * type: I am the type of error being thrown.
// * message: I am the reason the error is being thrown.
// * detail: I am an explanation of the error.
// * extendedInfo: I am additional information about the error context.
// * errorCode: I am a custom error code associated with this type of error.
// --
// The implementationContext argument is an optional argument that can be used to trim
// the generated stacktrace. If not provided, it defaults to EpubError.
function EpubError( settings, implementationContext ) {
// Ensure that settings exists to prevent reference errors.
settings = ( settings || {} );
// Override the default name property (Error). This is basically zero value-add.
this.name = "EpubError";
// Since I am used to ColdFusion, I am modeling the custom error structure on the
// CFThrow functionality. Each of the following properties can be optionally passed-in
// as part of the Settings argument.
// --
// See CFThrow documentation: https://wikidocs.adobe.com/wiki/display/coldfusionen/cfthrow
this.type = ( settings.type || "Default" );
this.message = ( settings.message || "An error occurred." );
this.detail = ( settings.detail || "" );
this.extendedInfo = ( settings.extendedInfo || "" );
this.errorCode = ( settings.errorCode || "" );
// This is just a flag that will indicate if the error is a custom EpubError. If this
// is not an EpubError, this property will be undefined, which is a Falsely.
this.isEpubError = true;
// Capture the current stacktrace and store it in the property "this.stack". By
// providing the implementationContext argument, we will remove the current
// constructor (or the optional factory function) line-item from the stacktrace; this
// is good because it will reduce the implementation noise in the stack property.
// --
// Read More: https://code.google.com/p/v8-wiki/wiki/JavaScriptStackTraceApi#Stack_trace_collection_for_custom_exceptions
Error.captureStackTrace( this, ( implementationContext || EpubError ) );
}
util.inherits( EpubError, Error );
module.exports = {
"EPUB#container": {
"missing": createEpubError({
type: "EPUB.MissingContainer",
message: "Container XML file is missing!",
errorCode: 404
}),
"format": createEpubError({
type: "EPUB.InvalidContainer",
"message": "Invalid Container XML file format"
})
},
"EPUB#opf": {
"missing": createEpubError({
type: "EPUB.MissingOpf",
message: "Opf file is missing!",
code: 404
}),
"spine": createEpubError({
type: "EPUB.InvalidSpine",
message: "Spine entry not found in Manifest TAG"
}),
"format": createEpubError({
type: "EPUB.InvalidOpf",
message: "Invalid opf file format"
})
},
"EPUB#spine": {
"missing": createEpubError({
type: "EPUB.MissingSpine",
message: "Opf file is missing!",
code: 404
}),
"spine": createEpubError({
type: "EPUB.InvalidSpine",
message: "Spine entry not found in Manifest TAG"
})
}
};