Permalink
Browse files

allow arrays of errors, allow warnings

  • Loading branch information...
coolaj86 committed Oct 8, 2012
1 parent 7a7080c commit 7858da317e6bb258fc846cc3842c1eee9871dd10
Showing with 46 additions and 19 deletions.
  1. +16 −2 http-json/README.md
  2. +29 −16 http-json/index.js
  3. +1 −1 http-json/package.json
View
@@ -22,6 +22,8 @@ Usage
res.error(new Error("What time is it? Error time!"));
res.json();
return;
+ } else {
+ res.warn(new Error("Did you know that 1 out of 3 times is error time?"));
}
res.json("What time is it? Success time!");
@@ -36,6 +38,7 @@ Response
{
"success": true
, "errors": []
+ , "warns": [{"message":"Did you know that 1 out of 3 times is error time?"}]
, "foo-secret": 42
, "timestamp": 1339631260781
, "result": "What time is it? Success time!"
@@ -64,11 +67,22 @@ Optional `params` may be an object such as this
http.ServerResponse.prototype.meta(key, value)
---
-Add a field to the "header" (sibling of `result`, `success`, etc)
+Add a field to the "header" (sibling of `result`, `errors`, etc)
+
+http.ServerResponse.prototype.warn(Error)
+---
+
+Add an Error to the `warns` array, but `success` is left as true.
+
+`Error` may also be `null`/`undefined` (ignored), an array of Errors, or an empty array (ignored).
+
+**Note**: By default, stack traces will be removed from warns.
http.ServerResponse.prototype.error(Error)
---
-Add an Error to the `error` array and set `success` to false.
+Add an Error to the `errors` array and set `success` to false.
+
+`Error` may also be `null`/`undefined` (ignored), an array of Errors, or an empty array (ignored).
**Note**: By default, stack traces will be removed from errors.
View
@@ -14,11 +14,24 @@
, resProto = http.ServerResponse.prototype
;
- resProto.warn = function (msg, code, opts) {
+ resProto._logger = function (level, msg, code, opts) {
var self = this
+ , _levels = '_' + level + 's'
;
- self._warns = self._warns || [];
+ self[_levels] = self[_levels] || [];
+
+ if (null === msg || undefined === msg || 0 === msg.length) {
+ return;
+ }
+
+ if (Array.isArray(msg)) {
+ msg.forEach(function (m) {
+ resProto._logger(level, m.error || m, m.code, m.opts || opts);
+ });
+ return;
+ }
+
if ('object' !== typeof opts) {
opts = {};
}
@@ -29,26 +42,26 @@
opts.message = msg;
opts.code = code;
- self._warns.push(opts);
+ self[_levels].push(opts);
};
- resProto.error = function (msg, code, opts) {
- var self = this
- ;
+ resProto.warn = function (msg, code, opts) {
+ resProto._logger('warn', msg, code, opts);
+ };
- self._errors = self._errors || [];
- if ('object' !== typeof opts) {
- opts = {};
- }
+ resProto.error = function (msg, code, opts) {
+ resProto._logger('error', msg, code, opts);
+ };
- if (msg instanceof Error) {
- msg = msg.message || msg.toString();
- }
+ /*
+ resProto.info = function (msg, code, opts) {
+ resProto._logger('info', msg, code, opts);
+ };
- opts.message = msg;
- opts.code = code;
- self._errors.push(opts);
+ resProto.debug = function (msg, code, opts) {
+ resProto._logger('info', msg, code, opts);
};
+ */
// For adding things like 'session'
resProto.meta = function (key, val) {
View
@@ -2,7 +2,7 @@
"author": "AJ ONeal <coolaj86@gmail.com> (http://coolaj86.info/)",
"name": "http-json",
"description": "An extension of the http prototype supporting steve-style json responses in connect",
- "version": "1.1.0",
+ "version": "1.2.0",
"homepage": "http://github.com/coolaj86/steve",
"repository": {
"type": "git",

0 comments on commit 7858da3

Please sign in to comment.