added whole system from ola4doc
This commit is contained in:
Generated
+1
@@ -0,0 +1 @@
|
||||
node_modules
|
||||
+19
@@ -0,0 +1,19 @@
|
||||
Copyright (c) 2012, Joyent, Inc. All rights reserved.
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in
|
||||
all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
THE SOFTWARE
|
||||
+35
@@ -0,0 +1,35 @@
|
||||
#
|
||||
# Copyright (c) 2012, Joyent, Inc. All rights reserved.
|
||||
#
|
||||
# Makefile: top-level Makefile
|
||||
#
|
||||
# This Makefile contains only repo-specific logic and uses included makefiles
|
||||
# to supply common targets (javascriptlint, jsstyle, restdown, etc.), which are
|
||||
# used by other repos as well.
|
||||
#
|
||||
|
||||
#
|
||||
# Tools
|
||||
#
|
||||
NPM = npm
|
||||
|
||||
#
|
||||
# Files
|
||||
#
|
||||
JS_FILES := $(shell find lib examples tests -name '*.js')
|
||||
JSL_FILES_NODE = $(JS_FILES)
|
||||
JSSTYLE_FILES = $(JS_FILES)
|
||||
JSL_CONF_NODE = jsl.node.conf
|
||||
|
||||
.PHONY: all
|
||||
all:
|
||||
$(NPM) install
|
||||
|
||||
.PHONY: test
|
||||
test:
|
||||
node tests/tst.inherit.js
|
||||
node tests/tst.verror.js
|
||||
node tests/tst.werror.js
|
||||
@echo all tests passed
|
||||
|
||||
include ./Makefile.targ
|
||||
+285
@@ -0,0 +1,285 @@
|
||||
# -*- mode: makefile -*-
|
||||
#
|
||||
# Copyright (c) 2012, Joyent, Inc. All rights reserved.
|
||||
#
|
||||
# Makefile.targ: common targets.
|
||||
#
|
||||
# NOTE: This makefile comes from the "eng" repo. It's designed to be dropped
|
||||
# into other repos as-is without requiring any modifications. If you find
|
||||
# yourself changing this file, you should instead update the original copy in
|
||||
# eng.git and then update your repo to use the new version.
|
||||
#
|
||||
# This Makefile defines several useful targets and rules. You can use it by
|
||||
# including it from a Makefile that specifies some of the variables below.
|
||||
#
|
||||
# Targets defined in this Makefile:
|
||||
#
|
||||
# check Checks JavaScript files for lint and style
|
||||
# Checks bash scripts for syntax
|
||||
# Checks SMF manifests for validity against the SMF DTD
|
||||
#
|
||||
# clean Removes built files
|
||||
#
|
||||
# docs Builds restdown documentation in docs/
|
||||
#
|
||||
# prepush Depends on "check" and "test"
|
||||
#
|
||||
# test Does nothing (you should override this)
|
||||
#
|
||||
# xref Generates cscope (source cross-reference index)
|
||||
#
|
||||
# For details on what these targets are supposed to do, see the Joyent
|
||||
# Engineering Guide.
|
||||
#
|
||||
# To make use of these targets, you'll need to set some of these variables. Any
|
||||
# variables left unset will simply not be used.
|
||||
#
|
||||
# BASH_FILES Bash scripts to check for syntax
|
||||
# (paths relative to top-level Makefile)
|
||||
#
|
||||
# CLEAN_FILES Files to remove as part of the "clean" target. Note
|
||||
# that files generated by targets in this Makefile are
|
||||
# automatically included in CLEAN_FILES. These include
|
||||
# restdown-generated HTML and JSON files.
|
||||
#
|
||||
# DOC_FILES Restdown (documentation source) files. These are
|
||||
# assumed to be contained in "docs/", and must NOT
|
||||
# contain the "docs/" prefix.
|
||||
#
|
||||
# JSL_CONF_NODE Specify JavaScriptLint configuration files
|
||||
# JSL_CONF_WEB (paths relative to top-level Makefile)
|
||||
#
|
||||
# Node.js and Web configuration files are separate
|
||||
# because you'll usually want different global variable
|
||||
# configurations. If no file is specified, none is given
|
||||
# to jsl, which causes it to use a default configuration,
|
||||
# which probably isn't what you want.
|
||||
#
|
||||
# JSL_FILES_NODE JavaScript files to check with Node config file.
|
||||
# JSL_FILES_WEB JavaScript files to check with Web config file.
|
||||
#
|
||||
# You can also override these variables:
|
||||
#
|
||||
# BASH Path to bash (default: bash)
|
||||
#
|
||||
# CSCOPE_DIRS Directories to search for source files for the cscope
|
||||
# index. (default: ".")
|
||||
#
|
||||
# JSL Path to JavaScriptLint (default: "jsl")
|
||||
#
|
||||
# JSL_FLAGS_NODE Additional flags to pass through to JSL
|
||||
# JSL_FLAGS_WEB
|
||||
# JSL_FLAGS
|
||||
#
|
||||
# JSSTYLE Path to jsstyle (default: jsstyle)
|
||||
#
|
||||
# JSSTYLE_FLAGS Additional flags to pass through to jsstyle
|
||||
#
|
||||
|
||||
#
|
||||
# Defaults for the various tools we use.
|
||||
#
|
||||
BASH ?= bash
|
||||
BASHSTYLE ?= tools/bashstyle
|
||||
CP ?= cp
|
||||
CSCOPE ?= cscope
|
||||
CSCOPE_DIRS ?= .
|
||||
JSL ?= jsl
|
||||
JSSTYLE ?= jsstyle
|
||||
MKDIR ?= mkdir -p
|
||||
MV ?= mv
|
||||
RESTDOWN_FLAGS ?=
|
||||
RMTREE ?= rm -rf
|
||||
JSL_FLAGS ?= --nologo --nosummary
|
||||
|
||||
ifeq ($(shell uname -s),SunOS)
|
||||
TAR ?= gtar
|
||||
else
|
||||
TAR ?= tar
|
||||
endif
|
||||
|
||||
|
||||
#
|
||||
# Defaults for other fixed values.
|
||||
#
|
||||
BUILD = build
|
||||
DISTCLEAN_FILES += $(BUILD)
|
||||
DOC_BUILD = $(BUILD)/docs/public
|
||||
|
||||
#
|
||||
# Configure JSL_FLAGS_{NODE,WEB} based on JSL_CONF_{NODE,WEB}.
|
||||
#
|
||||
ifneq ($(origin JSL_CONF_NODE), undefined)
|
||||
JSL_FLAGS_NODE += --conf=$(JSL_CONF_NODE)
|
||||
endif
|
||||
|
||||
ifneq ($(origin JSL_CONF_WEB), undefined)
|
||||
JSL_FLAGS_WEB += --conf=$(JSL_CONF_WEB)
|
||||
endif
|
||||
|
||||
#
|
||||
# Targets. For descriptions on what these are supposed to do, see the
|
||||
# Joyent Engineering Guide.
|
||||
#
|
||||
|
||||
#
|
||||
# Instruct make to keep around temporary files. We have rules below that
|
||||
# automatically update git submodules as needed, but they employ a deps/*/.git
|
||||
# temporary file. Without this directive, make tries to remove these .git
|
||||
# directories after the build has completed.
|
||||
#
|
||||
.SECONDARY: $($(wildcard deps/*):%=%/.git)
|
||||
|
||||
#
|
||||
# This rule enables other rules that use files from a git submodule to have
|
||||
# those files depend on deps/module/.git and have "make" automatically check
|
||||
# out the submodule as needed.
|
||||
#
|
||||
deps/%/.git:
|
||||
git submodule update --init deps/$*
|
||||
|
||||
#
|
||||
# These recipes make heavy use of dynamically-created phony targets. The parent
|
||||
# Makefile defines a list of input files like BASH_FILES. We then say that each
|
||||
# of these files depends on a fake target called filename.bashchk, and then we
|
||||
# define a pattern rule for those targets that runs bash in check-syntax-only
|
||||
# mode. This mechanism has the nice properties that if you specify zero files,
|
||||
# the rule becomes a noop (unlike a single rule to check all bash files, which
|
||||
# would invoke bash with zero files), and you can check individual files from
|
||||
# the command line with "make filename.bashchk".
|
||||
#
|
||||
.PHONY: check-bash
|
||||
check-bash: $(BASH_FILES:%=%.bashchk) $(BASH_FILES:%=%.bashstyle)
|
||||
|
||||
%.bashchk: %
|
||||
$(BASH) -n $^
|
||||
|
||||
%.bashstyle: %
|
||||
$(BASHSTYLE) $^
|
||||
|
||||
.PHONY: check-jsl check-jsl-node check-jsl-web
|
||||
check-jsl: check-jsl-node check-jsl-web
|
||||
|
||||
check-jsl-node: $(JSL_FILES_NODE:%=%.jslnodechk)
|
||||
|
||||
check-jsl-web: $(JSL_FILES_WEB:%=%.jslwebchk)
|
||||
|
||||
%.jslnodechk: % $(JSL_EXEC)
|
||||
$(JSL) $(JSL_FLAGS) $(JSL_FLAGS_NODE) $<
|
||||
|
||||
%.jslwebchk: % $(JSL_EXEC)
|
||||
$(JSL) $(JSL_FLAGS) $(JSL_FLAGS_WEB) $<
|
||||
|
||||
.PHONY: check-jsstyle
|
||||
check-jsstyle: $(JSSTYLE_FILES:%=%.jsstylechk)
|
||||
|
||||
%.jsstylechk: % $(JSSTYLE_EXEC)
|
||||
$(JSSTYLE) $(JSSTYLE_FLAGS) $<
|
||||
|
||||
.PHONY: check
|
||||
check: check-jsl check-jsstyle check-bash
|
||||
@echo check ok
|
||||
|
||||
.PHONY: clean
|
||||
clean::
|
||||
-$(RMTREE) $(CLEAN_FILES)
|
||||
|
||||
.PHONY: distclean
|
||||
distclean:: clean
|
||||
-$(RMTREE) $(DISTCLEAN_FILES)
|
||||
|
||||
CSCOPE_FILES = cscope.in.out cscope.out cscope.po.out
|
||||
CLEAN_FILES += $(CSCOPE_FILES)
|
||||
|
||||
.PHONY: xref
|
||||
xref: cscope.files
|
||||
$(CSCOPE) -bqR
|
||||
|
||||
.PHONY: cscope.files
|
||||
cscope.files:
|
||||
find $(CSCOPE_DIRS) -name '*.c' -o -name '*.h' -o -name '*.cc' \
|
||||
-o -name '*.js' -o -name '*.s' -o -name '*.cpp' > $@
|
||||
|
||||
#
|
||||
# The "docs" target is complicated because we do several things here:
|
||||
#
|
||||
# (1) Use restdown to build HTML and JSON files from each of DOC_FILES.
|
||||
#
|
||||
# (2) Copy these files into $(DOC_BUILD) (build/docs/public), which
|
||||
# functions as a complete copy of the documentation that could be
|
||||
# mirrored or served over HTTP.
|
||||
#
|
||||
# (3) Then copy any directories and media from docs/media into
|
||||
# $(DOC_BUILD)/media. This allows projects to include their own media,
|
||||
# including files that will override same-named files provided by
|
||||
# restdown.
|
||||
#
|
||||
# Step (3) is the surprisingly complex part: in order to do this, we need to
|
||||
# identify the subdirectories in docs/media, recreate them in
|
||||
# $(DOC_BUILD)/media, then do the same with the files.
|
||||
#
|
||||
DOC_MEDIA_DIRS := $(shell find docs/media -type d 2>/dev/null | grep -v "^docs/media$$")
|
||||
DOC_MEDIA_DIRS := $(DOC_MEDIA_DIRS:docs/media/%=%)
|
||||
DOC_MEDIA_DIRS_BUILD := $(DOC_MEDIA_DIRS:%=$(DOC_BUILD)/media/%)
|
||||
|
||||
DOC_MEDIA_FILES := $(shell find docs/media -type f 2>/dev/null)
|
||||
DOC_MEDIA_FILES := $(DOC_MEDIA_FILES:docs/media/%=%)
|
||||
DOC_MEDIA_FILES_BUILD := $(DOC_MEDIA_FILES:%=$(DOC_BUILD)/media/%)
|
||||
|
||||
#
|
||||
# Like the other targets, "docs" just depends on the final files we want to
|
||||
# create in $(DOC_BUILD), leveraging other targets and recipes to define how
|
||||
# to get there.
|
||||
#
|
||||
.PHONY: docs
|
||||
docs: \
|
||||
$(DOC_FILES:%.restdown=$(DOC_BUILD)/%.html) \
|
||||
$(DOC_FILES:%.restdown=$(DOC_BUILD)/%.json) \
|
||||
$(DOC_MEDIA_FILES_BUILD)
|
||||
|
||||
#
|
||||
# We keep the intermediate files so that the next build can see whether the
|
||||
# files in DOC_BUILD are up to date.
|
||||
#
|
||||
.PRECIOUS: \
|
||||
$(DOC_FILES:%.restdown=docs/%.html) \
|
||||
$(DOC_FILES:%.restdown=docs/%json)
|
||||
|
||||
#
|
||||
# We do clean those intermediate files, as well as all of DOC_BUILD.
|
||||
#
|
||||
CLEAN_FILES += \
|
||||
$(DOC_BUILD) \
|
||||
$(DOC_FILES:%.restdown=docs/%.html) \
|
||||
$(DOC_FILES:%.restdown=docs/%.json)
|
||||
|
||||
#
|
||||
# Before installing the files, we must make sure the directories exist. The |
|
||||
# syntax tells make that the dependency need only exist, not be up to date.
|
||||
# Otherwise, it might try to rebuild spuriously because the directory itself
|
||||
# appears out of date.
|
||||
#
|
||||
$(DOC_MEDIA_FILES_BUILD): | $(DOC_MEDIA_DIRS_BUILD)
|
||||
|
||||
$(DOC_BUILD)/%: docs/% | $(DOC_BUILD)
|
||||
$(CP) $< $@
|
||||
|
||||
docs/%.json docs/%.html: docs/%.restdown | $(DOC_BUILD) $(RESTDOWN_EXEC)
|
||||
$(RESTDOWN) $(RESTDOWN_FLAGS) -m $(DOC_BUILD) $<
|
||||
|
||||
$(DOC_BUILD):
|
||||
$(MKDIR) $@
|
||||
|
||||
$(DOC_MEDIA_DIRS_BUILD):
|
||||
$(MKDIR) $@
|
||||
|
||||
#
|
||||
# The default "test" target does nothing. This should usually be overridden by
|
||||
# the parent Makefile. It's included here so we can define "prepush" without
|
||||
# requiring the repo to define "test".
|
||||
#
|
||||
.PHONY: test
|
||||
test:
|
||||
|
||||
.PHONY: prepush
|
||||
prepush: check test
|
||||
+120
@@ -0,0 +1,120 @@
|
||||
# verror: richer JavaScript errors
|
||||
|
||||
This module provides two classes: VError, for accretive errors, and WError, for
|
||||
wrapping errors. Both support printf-style error messages using extsprintf.
|
||||
|
||||
## Printf-style errors
|
||||
|
||||
At the most basic level, VError is just like JavaScript's Error class, but with
|
||||
printf-style arguments:
|
||||
|
||||
var verror = require('verror');
|
||||
|
||||
var opname = 'read';
|
||||
var err = new verror.VError('"%s" operation failed', opname);
|
||||
console.log(err.message);
|
||||
console.log(err.stack);
|
||||
|
||||
This prints:
|
||||
|
||||
"read" operation failed
|
||||
"read" operation failed
|
||||
at Object.<anonymous> (/Users/dap/node-verror/examples/varargs.js:4:11)
|
||||
at Module._compile (module.js:449:26)
|
||||
at Object.Module._extensions..js (module.js:467:10)
|
||||
at Module.load (module.js:356:32)
|
||||
at Function.Module._load (module.js:312:12)
|
||||
at Module.runMain (module.js:492:10)
|
||||
at process.startup.processNextTick.process._tickCallback (node.js:244:9)
|
||||
|
||||
|
||||
## VError for accretive error messages
|
||||
|
||||
More interestingly, you can use VError to build up an error describing what
|
||||
happened at various levels in the stack. For example, suppose you have a
|
||||
request handler that stats a file and fails if it doesn't exist:
|
||||
|
||||
var fs = require('fs');
|
||||
var verror = require('verror');
|
||||
|
||||
function checkFile(filename, callback) {
|
||||
fs.stat(filename, function (err) {
|
||||
if (err)
|
||||
/* Annotate the "stat" error with what we were doing. */
|
||||
return (callback(new verror.VError(err,
|
||||
'failed to check "%s"', filename)));
|
||||
|
||||
/* ... */
|
||||
});
|
||||
}
|
||||
|
||||
function handleRequest(filename, callback) {
|
||||
checkFile('/nonexistent', function (err) {
|
||||
if (err) {
|
||||
/* Annotate the "checkFile" error with what we were doing. */
|
||||
return (callback(new verror.VError(err, 'request failed')));
|
||||
}
|
||||
|
||||
/* ... */
|
||||
});
|
||||
}
|
||||
|
||||
handleRequest('/nonexistent', function (err) {
|
||||
if (err)
|
||||
console.log(err.message);
|
||||
/* ... */
|
||||
});
|
||||
|
||||
Since the file "/nonexistent" doesn't exist, this prints out:
|
||||
|
||||
request failed: failed to check "/nonexistent": ENOENT, stat '/nonexistent'
|
||||
|
||||
The idea here is that the lowest level (Node's "fs.stat" function) generates an
|
||||
arbitrary error, and each higher level (request handler and stat callback)
|
||||
creates a new VError that annotates the previous error with what it was doing,
|
||||
so that the result is a clear message explaining what failed at each level.
|
||||
|
||||
This plays nicely with extsprintf's "%r" specifier, which prints out a
|
||||
Java-style stacktrace with the whole chain of exceptions:
|
||||
|
||||
EXCEPTION: VError: request failed: failed to check "/nonexistent": ENOENT, stat '/nonexistent'
|
||||
at /Users/dap/work/node-verror/examples/levels.js:21:21
|
||||
at /Users/dap/work/node-verror/examples/levels.js:9:12
|
||||
at Object.oncomplete (fs.js:297:15)
|
||||
Caused by: EXCEPTION: VError: failed to check "/nonexistent": ENOENT, stat '/nonexistent'
|
||||
at /Users/dap/work/node-verror/examples/levels.js:9:21
|
||||
at Object.oncomplete (fs.js:297:15)
|
||||
Caused by: EXCEPTION: Error: Error: ENOENT, stat '/nonexistent'
|
||||
|
||||
|
||||
## WError for wrapped errors
|
||||
|
||||
Sometimes you don't want an Error's "message" field to include the details of
|
||||
all of the low-level errors, but you still want to be able to get at them
|
||||
programmatically. For example, in an HTTP server, you probably don't want to
|
||||
spew all of the low-level errors back to the client, but you do want to include
|
||||
them in the audit log entry for the request. In that case, you can use a
|
||||
WError, which is created exactly like VError (and also supports both
|
||||
printf-style arguments and an optional cause), but the resulting "message" only
|
||||
contains the top-level error. It's also more verbose, including the class
|
||||
associated with each error in the cause chain. Using the same example above,
|
||||
but replacing the VError in handleRequest with WError, we get this output:
|
||||
|
||||
request failed
|
||||
|
||||
That's what we wanted -- just a high-level summary for the client. But we can
|
||||
get the object's toString() for the full details:
|
||||
|
||||
WError: request failed; caused by WError: failed to check "/nonexistent";
|
||||
caused by Error: ENOENT, stat '/nonexistent'
|
||||
|
||||
# Contributing
|
||||
|
||||
Contributions welcome. Code should be "make check" clean. To run "make check",
|
||||
you'll need these tools:
|
||||
|
||||
* https://github.com/davepacheco/jsstyle
|
||||
* https://github.com/davepacheco/javascriptlint
|
||||
|
||||
If you're changing something non-trivial or user-facing, you may want to submit
|
||||
an issue first.
|
||||
+36
@@ -0,0 +1,36 @@
|
||||
var extsprintf = require('extsprintf');
|
||||
var fs = require('fs');
|
||||
var verror = require('../lib/verror');
|
||||
|
||||
function checkFile(filename, callback) {
|
||||
fs.stat(filename, function (err) {
|
||||
if (err)
|
||||
/* Annotate the "stat" error with what we were doing. */
|
||||
return (callback(new verror.VError(err,
|
||||
'failed to check "%s"', filename)));
|
||||
|
||||
/* ... */
|
||||
return (callback());
|
||||
});
|
||||
}
|
||||
|
||||
function handleRequest(filename, callback) {
|
||||
checkFile('/nonexistent', function (err) {
|
||||
if (err)
|
||||
/* Annotate the "checkFile" error. */
|
||||
return (callback(new verror.VError(
|
||||
err, 'request failed')));
|
||||
|
||||
/* ... */
|
||||
return (callback());
|
||||
});
|
||||
}
|
||||
|
||||
handleRequest('/nonexistent', function (err) {
|
||||
if (err) {
|
||||
console.log(err.message);
|
||||
console.log(extsprintf.sprintf('%r', err));
|
||||
}
|
||||
|
||||
/* ... */
|
||||
});
|
||||
+34
@@ -0,0 +1,34 @@
|
||||
var extsprintf = require('extsprintf');
|
||||
var fs = require('fs');
|
||||
var verror = require('../lib/verror');
|
||||
|
||||
function checkFile(filename, callback) {
|
||||
fs.stat(filename, function (err) {
|
||||
if (err)
|
||||
/* Annotate the "stat" error with what we were doing. */
|
||||
return (callback(new verror.VError(err,
|
||||
'failed to check "%s"', filename)));
|
||||
|
||||
/* ... */
|
||||
return (callback());
|
||||
});
|
||||
}
|
||||
|
||||
function handleRequest(filename, callback) {
|
||||
checkFile('/nonexistent', function (err) {
|
||||
if (err)
|
||||
/* Wrap the "checkFile" error. */
|
||||
return (callback(new verror.WError(
|
||||
err, 'request failed')));
|
||||
|
||||
/* ... */
|
||||
return (callback());
|
||||
});
|
||||
}
|
||||
|
||||
handleRequest('/nonexistent', function (err) {
|
||||
if (err) {
|
||||
console.log(err.message);
|
||||
console.log(err.toString());
|
||||
}
|
||||
});
|
||||
+6
@@ -0,0 +1,6 @@
|
||||
var verror = require('../lib/verror');
|
||||
|
||||
var opname = 'read';
|
||||
var err = new verror.VError('"%s" operation failed', opname);
|
||||
console.log(err.message);
|
||||
console.log(err.stack);
|
||||
+13
@@ -0,0 +1,13 @@
|
||||
var mod_fs = require('fs');
|
||||
var mod_verror = require('../lib/verror');
|
||||
|
||||
var filename = '/nonexistent';
|
||||
|
||||
mod_fs.stat(filename, function (err1) {
|
||||
var err2 = new mod_verror.VError(err1, 'failed to stat "%s"', filename);
|
||||
|
||||
/* The following would normally be higher up the stack. */
|
||||
var err3 = new mod_verror.VError(err2, 'failed to handle request');
|
||||
console.log(err3.message);
|
||||
console.log(err3.stack);
|
||||
});
|
||||
+14
@@ -0,0 +1,14 @@
|
||||
var mod_fs = require('fs');
|
||||
var mod_verror = require('../lib/verror');
|
||||
|
||||
var filename = '/nonexistent';
|
||||
|
||||
mod_fs.stat(filename, function (err1) {
|
||||
var err2 = new mod_verror.WError(err1, 'failed to stat "%s"', filename);
|
||||
|
||||
/* The following would normally be higher up the stack. */
|
||||
var err3 = new mod_verror.WError(err2, 'failed to handle request');
|
||||
console.log(err3.message);
|
||||
console.log(err3.toString());
|
||||
console.log(err3.stack);
|
||||
});
|
||||
+139
@@ -0,0 +1,139 @@
|
||||
#
|
||||
# Configuration File for JavaScript Lint
|
||||
#
|
||||
# This configuration file can be used to lint a collection of scripts, or to enable
|
||||
# or disable warnings for scripts that are linted via the command line.
|
||||
#
|
||||
|
||||
### Warnings
|
||||
# Enable or disable warnings based on requirements.
|
||||
# Use "+WarningName" to display or "-WarningName" to suppress.
|
||||
#
|
||||
+ambiguous_else_stmt # the else statement could be matched with one of multiple if statements (use curly braces to indicate intent
|
||||
+ambiguous_nested_stmt # block statements containing block statements should use curly braces to resolve ambiguity
|
||||
+ambiguous_newline # unexpected end of line; it is ambiguous whether these lines are part of the same statement
|
||||
+anon_no_return_value # anonymous function does not always return value
|
||||
+assign_to_function_call # assignment to a function call
|
||||
-block_without_braces # block statement without curly braces
|
||||
+comma_separated_stmts # multiple statements separated by commas (use semicolons?)
|
||||
+comparison_type_conv # comparisons against null, 0, true, false, or an empty string allowing implicit type conversion (use === or !==)
|
||||
+default_not_at_end # the default case is not at the end of the switch statement
|
||||
+dup_option_explicit # duplicate "option explicit" control comment
|
||||
+duplicate_case_in_switch # duplicate case in switch statement
|
||||
+duplicate_formal # duplicate formal argument {name}
|
||||
+empty_statement # empty statement or extra semicolon
|
||||
+identifier_hides_another # identifer {name} hides an identifier in a parent scope
|
||||
-inc_dec_within_stmt # increment (++) and decrement (--) operators used as part of greater statement
|
||||
+incorrect_version # Expected /*jsl:content-type*/ control comment. The script was parsed with the wrong version.
|
||||
+invalid_fallthru # unexpected "fallthru" control comment
|
||||
+invalid_pass # unexpected "pass" control comment
|
||||
+jsl_cc_not_understood # couldn't understand control comment using /*jsl:keyword*/ syntax
|
||||
+leading_decimal_point # leading decimal point may indicate a number or an object member
|
||||
+legacy_cc_not_understood # couldn't understand control comment using /*@keyword@*/ syntax
|
||||
+meaningless_block # meaningless block; curly braces have no impact
|
||||
+mismatch_ctrl_comments # mismatched control comment; "ignore" and "end" control comments must have a one-to-one correspondence
|
||||
+misplaced_regex # regular expressions should be preceded by a left parenthesis, assignment, colon, or comma
|
||||
+missing_break # missing break statement
|
||||
+missing_break_for_last_case # missing break statement for last case in switch
|
||||
+missing_default_case # missing default case in switch statement
|
||||
+missing_option_explicit # the "option explicit" control comment is missing
|
||||
+missing_semicolon # missing semicolon
|
||||
+missing_semicolon_for_lambda # missing semicolon for lambda assignment
|
||||
+multiple_plus_minus # unknown order of operations for successive plus (e.g. x+++y) or minus (e.g. x---y) signs
|
||||
+nested_comment # nested comment
|
||||
+no_return_value # function {name} does not always return a value
|
||||
+octal_number # leading zeros make an octal number
|
||||
+parseint_missing_radix # parseInt missing radix parameter
|
||||
+partial_option_explicit # the "option explicit" control comment, if used, must be in the first script tag
|
||||
+redeclared_var # redeclaration of {name}
|
||||
+trailing_comma_in_array # extra comma is not recommended in array initializers
|
||||
+trailing_decimal_point # trailing decimal point may indicate a number or an object member
|
||||
+undeclared_identifier # undeclared identifier: {name}
|
||||
+unreachable_code # unreachable code
|
||||
-unreferenced_argument # argument declared but never referenced: {name}
|
||||
-unreferenced_function # function is declared but never referenced: {name}
|
||||
+unreferenced_variable # variable is declared but never referenced: {name}
|
||||
+unsupported_version # JavaScript {version} is not supported
|
||||
+use_of_label # use of label
|
||||
+useless_assign # useless assignment
|
||||
+useless_comparison # useless comparison; comparing identical expressions
|
||||
-useless_quotes # the quotation marks are unnecessary
|
||||
+useless_void # use of the void type may be unnecessary (void is always undefined)
|
||||
+var_hides_arg # variable {name} hides argument
|
||||
+want_assign_or_call # expected an assignment or function call
|
||||
+with_statement # with statement hides undeclared variables; use temporary variable instead
|
||||
|
||||
|
||||
### Output format
|
||||
# Customize the format of the error message.
|
||||
# __FILE__ indicates current file path
|
||||
# __FILENAME__ indicates current file name
|
||||
# __LINE__ indicates current line
|
||||
# __COL__ indicates current column
|
||||
# __ERROR__ indicates error message (__ERROR_PREFIX__: __ERROR_MSG__)
|
||||
# __ERROR_NAME__ indicates error name (used in configuration file)
|
||||
# __ERROR_PREFIX__ indicates error prefix
|
||||
# __ERROR_MSG__ indicates error message
|
||||
#
|
||||
# For machine-friendly output, the output format can be prefixed with
|
||||
# "encode:". If specified, all items will be encoded with C-slashes.
|
||||
#
|
||||
# Visual Studio syntax (default):
|
||||
+output-format __FILE__(__LINE__): __ERROR__
|
||||
# Alternative syntax:
|
||||
#+output-format __FILE__:__LINE__: __ERROR__
|
||||
|
||||
|
||||
### Context
|
||||
# Show the in-line position of the error.
|
||||
# Use "+context" to display or "-context" to suppress.
|
||||
#
|
||||
+context
|
||||
|
||||
|
||||
### Control Comments
|
||||
# Both JavaScript Lint and the JScript interpreter confuse each other with the syntax for
|
||||
# the /*@keyword@*/ control comments and JScript conditional comments. (The latter is
|
||||
# enabled in JScript with @cc_on@). The /*jsl:keyword*/ syntax is preferred for this reason,
|
||||
# although legacy control comments are enabled by default for backward compatibility.
|
||||
#
|
||||
-legacy_control_comments
|
||||
|
||||
|
||||
### Defining identifiers
|
||||
# By default, "option explicit" is enabled on a per-file basis.
|
||||
# To enable this for all files, use "+always_use_option_explicit"
|
||||
-always_use_option_explicit
|
||||
|
||||
# Define certain identifiers of which the lint is not aware.
|
||||
# (Use this in conjunction with the "undeclared identifier" warning.)
|
||||
#
|
||||
# Common uses for webpages might be:
|
||||
+define __dirname
|
||||
+define clearInterval
|
||||
+define clearTimeout
|
||||
+define console
|
||||
+define exports
|
||||
+define global
|
||||
+define process
|
||||
+define require
|
||||
+define setInterval
|
||||
+define setTimeout
|
||||
+define Buffer
|
||||
+define JSON
|
||||
+define Math
|
||||
+define __dirname
|
||||
+define __filename
|
||||
|
||||
### JavaScript Version
|
||||
# To change the default JavaScript version:
|
||||
#+default-type text/javascript;version=1.5
|
||||
#+default-type text/javascript;e4x=1
|
||||
|
||||
### Files
|
||||
# Specify which files to lint
|
||||
# Use "+recurse" to enable recursion (disabled by default).
|
||||
# To add a set of files, use "+process FileName", "+process Folder\Path\*.js",
|
||||
# or "+process Folder\Path\*.htm".
|
||||
#
|
||||
|
||||
+157
@@ -0,0 +1,157 @@
|
||||
/*
|
||||
* verror.js: richer JavaScript errors
|
||||
*/
|
||||
|
||||
var mod_assert = require('assert');
|
||||
var mod_util = require('util');
|
||||
|
||||
var mod_extsprintf = require('extsprintf');
|
||||
|
||||
/*
|
||||
* Public interface
|
||||
*/
|
||||
exports.VError = VError;
|
||||
exports.WError = WError;
|
||||
exports.MultiError = MultiError;
|
||||
|
||||
/*
|
||||
* Like JavaScript's built-in Error class, but supports a "cause" argument and a
|
||||
* printf-style message. The cause argument can be null.
|
||||
*/
|
||||
function VError(options)
|
||||
{
|
||||
var args, causedBy, ctor, tailmsg;
|
||||
|
||||
if (options instanceof Error || typeof (options) === 'object') {
|
||||
args = Array.prototype.slice.call(arguments, 1);
|
||||
} else {
|
||||
args = Array.prototype.slice.call(arguments, 0);
|
||||
options = undefined;
|
||||
}
|
||||
|
||||
tailmsg = args.length > 0 ?
|
||||
mod_extsprintf.sprintf.apply(null, args) : '';
|
||||
this.jse_shortmsg = tailmsg;
|
||||
this.jse_summary = tailmsg;
|
||||
|
||||
if (options) {
|
||||
causedBy = options.cause;
|
||||
|
||||
if (!causedBy || !(options.cause instanceof Error))
|
||||
causedBy = options;
|
||||
|
||||
if (causedBy && (causedBy instanceof Error)) {
|
||||
this.jse_cause = causedBy;
|
||||
this.jse_summary += ': ' + causedBy.message;
|
||||
}
|
||||
}
|
||||
|
||||
this.message = this.jse_summary;
|
||||
Error.call(this, this.jse_summary);
|
||||
|
||||
if (Error.captureStackTrace) {
|
||||
ctor = options ? options.constructorOpt : undefined;
|
||||
ctor = ctor || arguments.callee;
|
||||
Error.captureStackTrace(this, ctor);
|
||||
}
|
||||
}
|
||||
|
||||
mod_util.inherits(VError, Error);
|
||||
VError.prototype.name = 'VError';
|
||||
|
||||
VError.prototype.toString = function ve_toString()
|
||||
{
|
||||
var str = (this.hasOwnProperty('name') && this.name ||
|
||||
this.constructor.name || this.constructor.prototype.name);
|
||||
if (this.message)
|
||||
str += ': ' + this.message;
|
||||
|
||||
return (str);
|
||||
};
|
||||
|
||||
VError.prototype.cause = function ve_cause()
|
||||
{
|
||||
return (this.jse_cause);
|
||||
};
|
||||
|
||||
|
||||
/*
|
||||
* Represents a collection of errors for the purpose of consumers that generally
|
||||
* only deal with one error. Callers can extract the individual errors
|
||||
* contained in this object, but may also just treat it as a normal single
|
||||
* error, in which case a summary message will be printed.
|
||||
*/
|
||||
function MultiError(errors)
|
||||
{
|
||||
mod_assert.ok(errors.length > 0);
|
||||
this.ase_errors = errors;
|
||||
|
||||
VError.call(this, errors[0], 'first of %d error%s',
|
||||
errors.length, errors.length == 1 ? '' : 's');
|
||||
}
|
||||
|
||||
mod_util.inherits(MultiError, VError);
|
||||
|
||||
|
||||
|
||||
/*
|
||||
* Like JavaScript's built-in Error class, but supports a "cause" argument which
|
||||
* is wrapped, not "folded in" as with VError. Accepts a printf-style message.
|
||||
* The cause argument can be null.
|
||||
*/
|
||||
function WError(options)
|
||||
{
|
||||
Error.call(this);
|
||||
|
||||
var args, cause, ctor;
|
||||
if (typeof (options) === 'object') {
|
||||
args = Array.prototype.slice.call(arguments, 1);
|
||||
} else {
|
||||
args = Array.prototype.slice.call(arguments, 0);
|
||||
options = undefined;
|
||||
}
|
||||
|
||||
if (args.length > 0) {
|
||||
this.message = mod_extsprintf.sprintf.apply(null, args);
|
||||
} else {
|
||||
this.message = '';
|
||||
}
|
||||
|
||||
if (options) {
|
||||
if (options instanceof Error) {
|
||||
cause = options;
|
||||
} else {
|
||||
cause = options.cause;
|
||||
ctor = options.constructorOpt;
|
||||
}
|
||||
}
|
||||
|
||||
Error.captureStackTrace(this, ctor || this.constructor);
|
||||
if (cause)
|
||||
this.cause(cause);
|
||||
|
||||
}
|
||||
|
||||
mod_util.inherits(WError, Error);
|
||||
WError.prototype.name = 'WError';
|
||||
|
||||
|
||||
WError.prototype.toString = function we_toString()
|
||||
{
|
||||
var str = (this.hasOwnProperty('name') && this.name ||
|
||||
this.constructor.name || this.constructor.prototype.name);
|
||||
if (this.message)
|
||||
str += ': ' + this.message;
|
||||
if (this.we_cause && this.we_cause.message)
|
||||
str += '; caused by ' + this.we_cause.toString();
|
||||
|
||||
return (str);
|
||||
};
|
||||
|
||||
WError.prototype.cause = function we_cause(c)
|
||||
{
|
||||
if (c instanceof Error)
|
||||
this.we_cause = c;
|
||||
|
||||
return (this.we_cause);
|
||||
};
|
||||
+79
@@ -0,0 +1,79 @@
|
||||
{
|
||||
"_args": [
|
||||
[
|
||||
{
|
||||
"raw": "verror@1.3.6",
|
||||
"scope": null,
|
||||
"escapedName": "verror",
|
||||
"name": "verror",
|
||||
"rawSpec": "1.3.6",
|
||||
"spec": "1.3.6",
|
||||
"type": "version"
|
||||
},
|
||||
"/mnt/Data/bach/Documents/ola/OLA#4/OLA#4DOC/sys/node_modules/jsprim"
|
||||
]
|
||||
],
|
||||
"_from": "verror@1.3.6",
|
||||
"_id": "verror@1.3.6",
|
||||
"_inCache": true,
|
||||
"_location": "/verror",
|
||||
"_npmUser": {
|
||||
"name": "dap",
|
||||
"email": "dap@cs.brown.edu"
|
||||
},
|
||||
"_npmVersion": "1.1.65",
|
||||
"_phantomChildren": {},
|
||||
"_requested": {
|
||||
"raw": "verror@1.3.6",
|
||||
"scope": null,
|
||||
"escapedName": "verror",
|
||||
"name": "verror",
|
||||
"rawSpec": "1.3.6",
|
||||
"spec": "1.3.6",
|
||||
"type": "version"
|
||||
},
|
||||
"_requiredBy": [
|
||||
"/jsprim"
|
||||
],
|
||||
"_resolved": "https://registry.npmjs.org/verror/-/verror-1.3.6.tgz",
|
||||
"_shasum": "cff5df12946d297d2baaefaa2689e25be01c005c",
|
||||
"_shrinkwrap": null,
|
||||
"_spec": "verror@1.3.6",
|
||||
"_where": "/mnt/Data/bach/Documents/ola/OLA#4/OLA#4DOC/sys/node_modules/jsprim",
|
||||
"bugs": {
|
||||
"url": "https://github.com/davepacheco/node-verror/issues"
|
||||
},
|
||||
"dependencies": {
|
||||
"extsprintf": "1.0.2"
|
||||
},
|
||||
"description": "richer JavaScript errors",
|
||||
"devDependencies": {},
|
||||
"directories": {},
|
||||
"dist": {
|
||||
"shasum": "cff5df12946d297d2baaefaa2689e25be01c005c",
|
||||
"tarball": "https://registry.npmjs.org/verror/-/verror-1.3.6.tgz"
|
||||
},
|
||||
"engines": [
|
||||
"node >=0.6.0"
|
||||
],
|
||||
"homepage": "https://github.com/davepacheco/node-verror#readme",
|
||||
"main": "./lib/verror.js",
|
||||
"maintainers": [
|
||||
{
|
||||
"name": "dap",
|
||||
"email": "dap@cs.brown.edu"
|
||||
}
|
||||
],
|
||||
"name": "verror",
|
||||
"optionalDependencies": {},
|
||||
"readme": "# verror: richer JavaScript errors\n\nThis module provides two classes: VError, for accretive errors, and WError, for\nwrapping errors. Both support printf-style error messages using extsprintf.\n\n## Printf-style errors\n\nAt the most basic level, VError is just like JavaScript's Error class, but with\nprintf-style arguments:\n\n var verror = require('verror');\n\n var opname = 'read';\n var err = new verror.VError('\"%s\" operation failed', opname);\n console.log(err.message);\n console.log(err.stack);\n\nThis prints:\n\n \"read\" operation failed\n \"read\" operation failed\n at Object.<anonymous> (/Users/dap/node-verror/examples/varargs.js:4:11)\n at Module._compile (module.js:449:26)\n at Object.Module._extensions..js (module.js:467:10)\n at Module.load (module.js:356:32)\n at Function.Module._load (module.js:312:12)\n at Module.runMain (module.js:492:10)\n at process.startup.processNextTick.process._tickCallback (node.js:244:9)\n\n\n## VError for accretive error messages\n\nMore interestingly, you can use VError to build up an error describing what\nhappened at various levels in the stack. For example, suppose you have a\nrequest handler that stats a file and fails if it doesn't exist:\n\n var fs = require('fs');\n var verror = require('verror');\n\n function checkFile(filename, callback) {\n fs.stat(filename, function (err) {\n if (err)\n\t\t/* Annotate the \"stat\" error with what we were doing. */\n\t \treturn (callback(new verror.VError(err,\n\t\t 'failed to check \"%s\"', filename)));\n\n\t /* ... */\n });\n }\n\n function handleRequest(filename, callback) {\n \tcheckFile('/nonexistent', function (err) {\n \t if (err) {\n \t \t/* Annotate the \"checkFile\" error with what we were doing. */\n \t \treturn (callback(new verror.VError(err, 'request failed')));\n \t }\n\n \t /* ... */\n \t});\n }\n\n handleRequest('/nonexistent', function (err) {\n\tif (err)\n\t\tconsole.log(err.message);\n\t/* ... */\n });\n\nSince the file \"/nonexistent\" doesn't exist, this prints out:\n\n request failed: failed to check \"/nonexistent\": ENOENT, stat '/nonexistent'\n\nThe idea here is that the lowest level (Node's \"fs.stat\" function) generates an\narbitrary error, and each higher level (request handler and stat callback)\ncreates a new VError that annotates the previous error with what it was doing,\nso that the result is a clear message explaining what failed at each level.\n\nThis plays nicely with extsprintf's \"%r\" specifier, which prints out a\nJava-style stacktrace with the whole chain of exceptions:\n\n EXCEPTION: VError: request failed: failed to check \"/nonexistent\": ENOENT, stat '/nonexistent'\n at /Users/dap/work/node-verror/examples/levels.js:21:21\n at /Users/dap/work/node-verror/examples/levels.js:9:12\n at Object.oncomplete (fs.js:297:15)\n Caused by: EXCEPTION: VError: failed to check \"/nonexistent\": ENOENT, stat '/nonexistent'\n at /Users/dap/work/node-verror/examples/levels.js:9:21\n at Object.oncomplete (fs.js:297:15)\n Caused by: EXCEPTION: Error: Error: ENOENT, stat '/nonexistent'\n\n\n## WError for wrapped errors\n\nSometimes you don't want an Error's \"message\" field to include the details of\nall of the low-level errors, but you still want to be able to get at them\nprogrammatically. For example, in an HTTP server, you probably don't want to\nspew all of the low-level errors back to the client, but you do want to include\nthem in the audit log entry for the request. In that case, you can use a\nWError, which is created exactly like VError (and also supports both\nprintf-style arguments and an optional cause), but the resulting \"message\" only\ncontains the top-level error. It's also more verbose, including the class\nassociated with each error in the cause chain. Using the same example above,\nbut replacing the VError in handleRequest with WError, we get this output:\n\n request failed\n\nThat's what we wanted -- just a high-level summary for the client. But we can\nget the object's toString() for the full details:\n\n WError: request failed; caused by WError: failed to check \"/nonexistent\";\n caused by Error: ENOENT, stat '/nonexistent'\n\n# Contributing\n\nContributions welcome. Code should be \"make check\" clean. To run \"make check\",\nyou'll need these tools:\n\n* https://github.com/davepacheco/jsstyle\n* https://github.com/davepacheco/javascriptlint\n\nIf you're changing something non-trivial or user-facing, you may want to submit\nan issue first.\n",
|
||||
"readmeFilename": "README.md",
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "git://github.com/davepacheco/node-verror.git"
|
||||
},
|
||||
"scripts": {
|
||||
"test": "make test"
|
||||
},
|
||||
"version": "1.3.6"
|
||||
}
|
||||
+100
@@ -0,0 +1,100 @@
|
||||
/*
|
||||
* tst.inherit.js: test that inheriting from VError and WError work as expected.
|
||||
*/
|
||||
|
||||
var mod_assert = require('assert');
|
||||
var mod_util = require('util');
|
||||
|
||||
var mod_verror = require('../lib/verror');
|
||||
|
||||
var VError = mod_verror.VError;
|
||||
var WError = mod_verror.WError;
|
||||
var err, suberr;
|
||||
|
||||
function VErrorChild()
|
||||
{
|
||||
VError.apply(this, Array.prototype.slice.call(arguments));
|
||||
}
|
||||
|
||||
mod_util.inherits(VErrorChild, VError);
|
||||
VErrorChild.prototype.name = 'VErrorChild';
|
||||
|
||||
|
||||
function WErrorChild()
|
||||
{
|
||||
WError.apply(this, Array.prototype.slice.call(arguments));
|
||||
}
|
||||
|
||||
mod_util.inherits(WErrorChild, WError);
|
||||
WErrorChild.prototype.name = 'WErrorChild';
|
||||
|
||||
|
||||
suberr = new Error('root cause');
|
||||
err = new VErrorChild(suberr, 'top');
|
||||
mod_assert.ok(err instanceof Error);
|
||||
mod_assert.ok(err instanceof VError);
|
||||
mod_assert.ok(err instanceof VErrorChild);
|
||||
mod_assert.equal(err.cause(), suberr);
|
||||
mod_assert.equal(err.message, 'top: root cause');
|
||||
mod_assert.equal(err.toString(), 'VErrorChild: top: root cause');
|
||||
mod_assert.equal(err.stack.split('\n')[0], 'VErrorChild: top: root cause');
|
||||
|
||||
suberr = new Error('root cause');
|
||||
err = new WErrorChild(suberr, 'top');
|
||||
mod_assert.ok(err instanceof Error);
|
||||
mod_assert.ok(err instanceof WError);
|
||||
mod_assert.ok(err instanceof WErrorChild);
|
||||
mod_assert.equal(err.cause(), suberr);
|
||||
mod_assert.equal(err.message, 'top');
|
||||
mod_assert.equal(err.toString(),
|
||||
'WErrorChild: top; caused by Error: root cause');
|
||||
mod_assert.equal(err.stack.split('\n')[0],
|
||||
'WErrorChild: top; caused by Error: root cause');
|
||||
|
||||
|
||||
// Test that `<Ctor>.toString()` uses the ctor name. I.e. setting
|
||||
// `<Ctor>.prototype.name` isn't necessary.
|
||||
function VErrorChildNoName() {
|
||||
VError.apply(this, Array.prototype.slice.call(arguments));
|
||||
}
|
||||
mod_util.inherits(VErrorChildNoName, VError);
|
||||
err = new VErrorChildNoName('top');
|
||||
mod_assert.equal(err.toString(), 'VErrorChildNoName: top');
|
||||
|
||||
function WErrorChildNoName() {
|
||||
WError.apply(this, Array.prototype.slice.call(arguments));
|
||||
}
|
||||
mod_util.inherits(WErrorChildNoName, WError);
|
||||
err = new WErrorChildNoName('top');
|
||||
mod_assert.equal(err.toString(), 'WErrorChildNoName: top');
|
||||
|
||||
|
||||
// Test that `<Ctor>.prototype.name` can be used for the `.toString()`
|
||||
// when the ctor is anonymous.
|
||||
var VErrorChildAnon = function () {
|
||||
VError.apply(this, Array.prototype.slice.call(arguments));
|
||||
};
|
||||
mod_util.inherits(VErrorChildAnon, VError);
|
||||
VErrorChildAnon.prototype.name = 'VErrorChildAnon';
|
||||
err = new VErrorChildAnon('top');
|
||||
mod_assert.equal(err.toString(), 'VErrorChildAnon: top');
|
||||
|
||||
var WErrorChildAnon = function () {
|
||||
WError.apply(this, Array.prototype.slice.call(arguments));
|
||||
};
|
||||
mod_util.inherits(WErrorChildAnon, WError);
|
||||
WErrorChildAnon.prototype.name = 'WErrorChildAnon';
|
||||
err = new WErrorChildAnon('top');
|
||||
mod_assert.equal(err.toString(), 'WErrorChildAnon: top');
|
||||
|
||||
|
||||
// Test get appropriate exception name in `.toString()` when reconstituting
|
||||
// an error instance a la:
|
||||
// https://github.com/mcavage/node-fast/blob/master/lib/client.js#L215
|
||||
err = new VError('top');
|
||||
err.name = 'CustomNameError';
|
||||
mod_assert.equal(err.toString(), 'CustomNameError: top');
|
||||
|
||||
err = new WError('top');
|
||||
err.name = 'CustomNameError';
|
||||
mod_assert.equal(err.toString(), 'CustomNameError: top');
|
||||
+156
@@ -0,0 +1,156 @@
|
||||
/*
|
||||
* tst.verror.js: tests basic functionality of the VError class.
|
||||
*/
|
||||
|
||||
var mod_assert = require('assert');
|
||||
var mod_verror = require('../lib/verror');
|
||||
|
||||
var VError = mod_verror.VError;
|
||||
var WError = mod_verror.WError;
|
||||
|
||||
var err, suberr, stack, substack;
|
||||
|
||||
/*
|
||||
* Remove full paths and relative line numbers from stack traces so that we can
|
||||
* compare against "known-good" output.
|
||||
*/
|
||||
function cleanStack(stacktxt)
|
||||
{
|
||||
var re = new RegExp(__filename + ':\\d+:\\d+', 'gm');
|
||||
stacktxt = stacktxt.replace(re, 'tst.verror.js');
|
||||
return (stacktxt);
|
||||
}
|
||||
|
||||
/*
|
||||
* Save the generic parts of all stack traces so we can avoid hardcoding
|
||||
* Node-specific implementation details in our testing of stack traces.
|
||||
*/
|
||||
var nodestack = new Error().stack.split('\n').slice(2).join('\n');
|
||||
|
||||
/* no arguments */
|
||||
err = new VError();
|
||||
mod_assert.equal(err.name, 'VError');
|
||||
mod_assert.ok(err instanceof Error);
|
||||
mod_assert.ok(err instanceof VError);
|
||||
mod_assert.equal(err.message, '');
|
||||
mod_assert.ok(err.cause() === undefined);
|
||||
stack = cleanStack(err.stack);
|
||||
mod_assert.equal(stack, [
|
||||
'VError',
|
||||
' at Object.<anonymous> (tst.verror.js)'
|
||||
].join('\n') + '\n' + nodestack);
|
||||
|
||||
/* options-argument form */
|
||||
err = new VError({});
|
||||
mod_assert.equal(err.message, '');
|
||||
mod_assert.ok(err.cause() === undefined);
|
||||
|
||||
/* simple message */
|
||||
err = new VError('my error');
|
||||
mod_assert.equal(err.message, 'my error');
|
||||
mod_assert.ok(err.cause() === undefined);
|
||||
stack = cleanStack(err.stack);
|
||||
mod_assert.equal(stack, [
|
||||
'VError: my error',
|
||||
' at Object.<anonymous> (tst.verror.js)'
|
||||
].join('\n') + '\n' + nodestack);
|
||||
|
||||
err = new VError({}, 'my error');
|
||||
mod_assert.equal(err.message, 'my error');
|
||||
mod_assert.ok(err.cause() === undefined);
|
||||
|
||||
/* printf-style message */
|
||||
err = new VError('%s error: %3d problems', 'very bad', 15);
|
||||
mod_assert.equal(err.message, 'very bad error: 15 problems');
|
||||
mod_assert.ok(err.cause() === undefined);
|
||||
|
||||
err = new VError({}, '%s error: %3d problems', 'very bad', 15);
|
||||
mod_assert.equal(err.message, 'very bad error: 15 problems');
|
||||
mod_assert.ok(err.cause() === undefined);
|
||||
|
||||
/* caused by another error, with no additional message */
|
||||
suberr = new Error('root cause');
|
||||
err = new VError(suberr);
|
||||
mod_assert.equal(err.message, ': root cause');
|
||||
mod_assert.ok(err.cause() === suberr);
|
||||
|
||||
err = new VError({ 'cause': suberr });
|
||||
mod_assert.equal(err.message, ': root cause');
|
||||
mod_assert.ok(err.cause() === suberr);
|
||||
|
||||
/* caused by another error, with annotation */
|
||||
err = new VError(suberr, 'proximate cause: %d issues', 3);
|
||||
mod_assert.equal(err.message, 'proximate cause: 3 issues: root cause');
|
||||
mod_assert.ok(err.cause() === suberr);
|
||||
stack = cleanStack(err.stack);
|
||||
mod_assert.equal(stack, [
|
||||
'VError: proximate cause: 3 issues: root cause',
|
||||
' at Object.<anonymous> (tst.verror.js)'
|
||||
].join('\n') + '\n' + nodestack);
|
||||
|
||||
err = new VError({ 'cause': suberr }, 'proximate cause: %d issues', 3);
|
||||
mod_assert.equal(err.message, 'proximate cause: 3 issues: root cause');
|
||||
mod_assert.ok(err.cause() === suberr);
|
||||
stack = cleanStack(err.stack);
|
||||
mod_assert.equal(stack, [
|
||||
'VError: proximate cause: 3 issues: root cause',
|
||||
' at Object.<anonymous> (tst.verror.js)'
|
||||
].join('\n') + '\n' + nodestack);
|
||||
|
||||
/* caused by another VError, with annotation. */
|
||||
suberr = err;
|
||||
err = new VError(suberr, 'top');
|
||||
mod_assert.equal(err.message, 'top: proximate cause: 3 issues: root cause');
|
||||
mod_assert.ok(err.cause() === suberr);
|
||||
|
||||
err = new VError({ 'cause': suberr }, 'top');
|
||||
mod_assert.equal(err.message, 'top: proximate cause: 3 issues: root cause');
|
||||
mod_assert.ok(err.cause() === suberr);
|
||||
|
||||
/* caused by a WError */
|
||||
suberr = new WError(new Error('root cause'), 'mid');
|
||||
err = new VError(suberr, 'top');
|
||||
mod_assert.equal(err.message, 'top: mid');
|
||||
mod_assert.ok(err.cause() === suberr);
|
||||
|
||||
/* null cause (for backwards compatibility with older versions) */
|
||||
err = new VError(null, 'my error');
|
||||
mod_assert.equal(err.message, 'my error');
|
||||
mod_assert.ok(err.cause() === undefined);
|
||||
stack = cleanStack(err.stack);
|
||||
mod_assert.equal(stack, [
|
||||
'VError: my error',
|
||||
' at Object.<anonymous> (tst.verror.js)'
|
||||
].join('\n') + '\n' + nodestack);
|
||||
|
||||
err = new VError({ 'cause': null }, 'my error');
|
||||
mod_assert.equal(err.message, 'my error');
|
||||
mod_assert.ok(err.cause() === undefined);
|
||||
|
||||
err = new VError(null);
|
||||
mod_assert.equal(err.message, '');
|
||||
mod_assert.ok(err.cause() === undefined);
|
||||
stack = cleanStack(err.stack);
|
||||
mod_assert.equal(stack, [
|
||||
'VError',
|
||||
' at Object.<anonymous> (tst.verror.js)'
|
||||
].join('\n') + '\n' + nodestack);
|
||||
|
||||
/* constructorOpt */
|
||||
function makeErr(options) {
|
||||
return (new VError(options, 'test error'));
|
||||
}
|
||||
err = makeErr({});
|
||||
stack = cleanStack(err.stack);
|
||||
mod_assert.equal(stack, [
|
||||
'VError: test error',
|
||||
' at makeErr (tst.verror.js)',
|
||||
' at Object.<anonymous> (tst.verror.js)'
|
||||
].join('\n') + '\n' + nodestack);
|
||||
|
||||
err = makeErr({ 'constructorOpt': makeErr });
|
||||
stack = cleanStack(err.stack);
|
||||
mod_assert.equal(stack, [
|
||||
'VError: test error',
|
||||
' at Object.<anonymous> (tst.verror.js)'
|
||||
].join('\n') + '\n' + nodestack);
|
||||
+179
@@ -0,0 +1,179 @@
|
||||
/*
|
||||
* tst.werror.js: tests basic functionality of the WError class.
|
||||
*/
|
||||
|
||||
var mod_assert = require('assert');
|
||||
var mod_verror = require('../lib/verror');
|
||||
|
||||
var VError = mod_verror.VError;
|
||||
var WError = mod_verror.WError;
|
||||
|
||||
var err, suberr, stack, substack;
|
||||
|
||||
/*
|
||||
* Remove full paths and relative line numbers from stack traces so that we can
|
||||
* compare against "known-good" output.
|
||||
*/
|
||||
function cleanStack(stacktxt)
|
||||
{
|
||||
var re = new RegExp(__filename + ':\\d+:\\d+', 'gm');
|
||||
stacktxt = stacktxt.replace(re, 'tst.werror.js');
|
||||
return (stacktxt);
|
||||
}
|
||||
|
||||
/*
|
||||
* Save the generic parts of all stack traces so we can avoid hardcoding
|
||||
* Node-specific implementation details in our testing of stack traces.
|
||||
*/
|
||||
var nodestack = new Error().stack.split('\n').slice(2).join('\n');
|
||||
|
||||
/* no arguments */
|
||||
err = new WError();
|
||||
mod_assert.equal(err.name, 'WError');
|
||||
mod_assert.ok(err instanceof Error);
|
||||
mod_assert.ok(err instanceof WError);
|
||||
mod_assert.equal(err.message, '');
|
||||
mod_assert.equal(err.toString(), 'WError');
|
||||
mod_assert.ok(err.cause() === undefined);
|
||||
stack = cleanStack(err.stack);
|
||||
mod_assert.equal(stack, [
|
||||
'WError',
|
||||
' at Object.<anonymous> (tst.werror.js)'
|
||||
].join('\n') + '\n' + nodestack);
|
||||
|
||||
/* options-argument form */
|
||||
err = new WError({});
|
||||
mod_assert.equal(err.message, '');
|
||||
mod_assert.equal(err.toString(), 'WError');
|
||||
mod_assert.ok(err.cause() === undefined);
|
||||
|
||||
/* simple message */
|
||||
err = new WError('my error');
|
||||
mod_assert.equal(err.message, 'my error');
|
||||
mod_assert.equal(err.toString(), 'WError: my error');
|
||||
mod_assert.ok(err.cause() === undefined);
|
||||
stack = cleanStack(err.stack);
|
||||
mod_assert.equal(stack, [
|
||||
'WError: my error',
|
||||
' at Object.<anonymous> (tst.werror.js)'
|
||||
].join('\n') + '\n' + nodestack);
|
||||
|
||||
err = new WError({}, 'my error');
|
||||
mod_assert.equal(err.message, 'my error');
|
||||
mod_assert.equal(err.toString(), 'WError: my error');
|
||||
mod_assert.ok(err.cause() === undefined);
|
||||
|
||||
/* printf-style message */
|
||||
err = new WError('%s error: %3d problems', 'very bad', 15);
|
||||
mod_assert.equal(err.message, 'very bad error: 15 problems');
|
||||
mod_assert.equal(err.toString(), 'WError: very bad error: 15 problems');
|
||||
mod_assert.ok(err.cause() === undefined);
|
||||
|
||||
err = new WError({}, '%s error: %3d problems', 'very bad', 15);
|
||||
mod_assert.equal(err.message, 'very bad error: 15 problems');
|
||||
mod_assert.equal(err.toString(), 'WError: very bad error: 15 problems');
|
||||
mod_assert.ok(err.cause() === undefined);
|
||||
|
||||
/* caused by another error, with no additional message */
|
||||
suberr = new Error('root cause');
|
||||
err = new WError(suberr);
|
||||
mod_assert.equal(err.message, '');
|
||||
mod_assert.equal(err.toString(), 'WError; caused by Error: root cause');
|
||||
mod_assert.ok(err.cause() === suberr);
|
||||
|
||||
err = new WError({ 'cause': suberr });
|
||||
mod_assert.equal(err.message, '');
|
||||
mod_assert.equal(err.toString(), 'WError; caused by Error: root cause');
|
||||
mod_assert.ok(err.cause() === suberr);
|
||||
|
||||
/* caused by another error, with annotation */
|
||||
err = new WError(suberr, 'proximate cause: %d issues', 3);
|
||||
mod_assert.equal(err.message, 'proximate cause: 3 issues');
|
||||
mod_assert.equal(err.toString(), 'WError: proximate cause: 3 issues; ' +
|
||||
'caused by Error: root cause');
|
||||
mod_assert.ok(err.cause() === suberr);
|
||||
stack = cleanStack(err.stack);
|
||||
mod_assert.equal(stack, [
|
||||
'WError: proximate cause: 3 issues; caused by Error: root cause',
|
||||
' at Object.<anonymous> (tst.werror.js)'
|
||||
].join('\n') + '\n' + nodestack);
|
||||
|
||||
err = new WError({ 'cause': suberr }, 'proximate cause: %d issues', 3);
|
||||
mod_assert.equal(err.message, 'proximate cause: 3 issues');
|
||||
mod_assert.equal(err.toString(), 'WError: proximate cause: 3 issues; ' +
|
||||
'caused by Error: root cause');
|
||||
mod_assert.ok(err.cause() === suberr);
|
||||
stack = cleanStack(err.stack);
|
||||
mod_assert.equal(stack, [
|
||||
'WError: proximate cause: 3 issues; caused by Error: root cause',
|
||||
' at Object.<anonymous> (tst.werror.js)'
|
||||
].join('\n') + '\n' + nodestack);
|
||||
|
||||
/* caused by another WError, with annotation. */
|
||||
suberr = err;
|
||||
err = new WError(suberr, 'top');
|
||||
mod_assert.equal(err.message, 'top');
|
||||
mod_assert.equal(err.toString(), 'WError: top; caused by WError: ' +
|
||||
'proximate cause: 3 issues; caused by Error: root cause');
|
||||
mod_assert.ok(err.cause() === suberr);
|
||||
|
||||
err = new WError({ 'cause': suberr }, 'top');
|
||||
mod_assert.equal(err.message, 'top');
|
||||
mod_assert.equal(err.toString(), 'WError: top; caused by WError: ' +
|
||||
'proximate cause: 3 issues; caused by Error: root cause');
|
||||
mod_assert.ok(err.cause() === suberr);
|
||||
|
||||
/* caused by a VError */
|
||||
suberr = new VError(new Error('root cause'), 'mid');
|
||||
err = new WError(suberr, 'top');
|
||||
mod_assert.equal(err.message, 'top');
|
||||
mod_assert.equal(err.toString(),
|
||||
'WError: top; caused by VError: mid: root cause');
|
||||
mod_assert.ok(err.cause() === suberr);
|
||||
|
||||
/* null cause (for backwards compatibility with older versions) */
|
||||
err = new WError(null, 'my error');
|
||||
mod_assert.equal(err.message, 'my error');
|
||||
mod_assert.equal(err.toString(), 'WError: my error');
|
||||
mod_assert.ok(err.cause() === undefined);
|
||||
stack = cleanStack(err.stack);
|
||||
mod_assert.equal(stack, [
|
||||
'WError: my error',
|
||||
' at Object.<anonymous> (tst.werror.js)'
|
||||
].join('\n') + '\n' + nodestack);
|
||||
|
||||
err = new WError({ 'cause': null }, 'my error');
|
||||
mod_assert.equal(err.message, 'my error');
|
||||
mod_assert.equal(err.toString(), 'WError: my error');
|
||||
mod_assert.ok(err.cause() === undefined);
|
||||
|
||||
err = new WError(null);
|
||||
mod_assert.equal(err.message, '');
|
||||
mod_assert.equal(err.toString(), 'WError');
|
||||
mod_assert.ok(err.cause() === undefined);
|
||||
stack = cleanStack(err.stack);
|
||||
mod_assert.equal(stack, [
|
||||
'WError',
|
||||
' at Object.<anonymous> (tst.werror.js)'
|
||||
].join('\n') + '\n' + nodestack);
|
||||
|
||||
/* constructorOpt */
|
||||
function makeErr(options) {
|
||||
return (new WError(options, 'test error'));
|
||||
}
|
||||
err = makeErr({});
|
||||
mod_assert.equal(err.toString(), 'WError: test error');
|
||||
stack = cleanStack(err.stack);
|
||||
mod_assert.equal(stack, [
|
||||
'WError: test error',
|
||||
' at makeErr (tst.werror.js)',
|
||||
' at Object.<anonymous> (tst.werror.js)'
|
||||
].join('\n') + '\n' + nodestack);
|
||||
|
||||
err = makeErr({ 'constructorOpt': makeErr });
|
||||
mod_assert.equal(err.toString(), 'WError: test error');
|
||||
stack = cleanStack(err.stack);
|
||||
mod_assert.equal(stack, [
|
||||
'WError: test error',
|
||||
' at Object.<anonymous> (tst.werror.js)'
|
||||
].join('\n') + '\n' + nodestack);
|
||||
Reference in New Issue
Block a user