Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 36
1.0.0#1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Uh oh!
There was an error while loading. Please reload this page.
1.0.0 #1
Changes from all commits
b7a0f86dac5e7b02dc50752f0bdf4a485b6f3bf38114eb2afa0899f4db9fb41c5bc354048d71b34555be28bf668efff74ba73bf3742c6e824164e04a3ce3ab46e409be393d14e2cafed1170c9ca730c2f5a5b3798e20deadd378a0145c57c955e5bee069850d0a2b10File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| { | ||
| "extends": "loopback", | ||
| "rules": { | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| sudo: false | ||
| language: node_js | ||
| node_js: | ||
| - "6" | ||
| - "4" | ||
| - "0.10" | ||
| - "0.12" | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| Copyright (c) IBM Corp. 2011,2016. All Rights Reserved. | ||
| Node module: strong-error-handler | ||
| This project is licensed under the MIT License, full text below. | ||
| -------- | ||
| MIT license | ||
| 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. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1 +1,4 @@ | ||
| # strong-error-handler | ||
| # strong-error-handler | ||
| # Install | ||
| ```$ npm install strong-error-handler``` |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,240 @@ | ||
| /*! | ||
| * strong-error-handler | ||
| * Copyright(c) 2015-2016 strongloop | ||
| * MIT Licensed | ||
| */ | ||
Member There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Uh. The header says MIT licensed, the license file says different. Note that we are expected to use IBM headers now, here is an example from loopback-datasource-juggler: // Copyright IBM Corp. 2015,2016. All Rights Reserved.// Node module: loopback-datasource-juggler// This file is licensed under the MIT License.// License text available at https://opensource.org/licenses/MIT | ||
| 'use strict'; | ||
| var accepts = require('accepts'); | ||
| var escapeHtml = require('escape-html'); | ||
| var fs = require('fs'); | ||
| var util = require('util'); | ||
| var DOUBLE_SPACE_REGEXP = / /g; | ||
| var inspect = util.inspect; | ||
| var NEW_LINE_GLOBAL_REGEXP = /\r?\n/; | ||
| var toString = Object.prototype.toString; | ||
| //var defer = function(fn) { process.nextTick(fn.bind.apply(fn, arguments)); }; | ||
| /** | ||
| * Strong Error handler: | ||
| * | ||
| * Production error handler, providing stack traces for debugging | ||
| * environment and error message responses for requests accepting text, html, | ||
| * or json. strong-error-handler will only provide safeFields in production | ||
| * mode. | ||
| * | ||
| * Text: | ||
| * | ||
| * By default, and when _text/plain_ is accepted a simple stack trace | ||
| * or error message will be returned. | ||
| * | ||
| * JSON: | ||
| * | ||
| * When _application/json_ is accepted, connect will respond with | ||
| * an object in the form of `{ "error": error }`. | ||
| * | ||
| * HTML: | ||
| * | ||
| * When accepted connect will output a nice html stack trace, | ||
| * or error message/description in Production | ||
| * | ||
Member There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What happens when the user agent does not specify any of those three content types as acceptable? ContributorAuthor There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That will default into plain text. 2 test cases are added. One with undefined type and one with XML. Member There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I remember seeing a plain text response for | ||
| * @return {Function} | ||
| * @api public | ||
| */ | ||
| exports = module.exports = function strongErrorHandler(options) { | ||
| // declaring options | ||
| options = options || {}; | ||
| // debugging mode is disabled by default. | ||
| // In dev, all error properties (including) stack traces | ||
| // are sent in the response | ||
| var debug = options.debug; | ||
| // get options | ||
| // get log option | ||
| function NOOP() {} | ||
| var log = options.log !== false ? | ||
| logerror : | ||
| NOOP; | ||
| if (log === true) { | ||
| log = logerror; | ||
Member There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As we discussed on Slack, please either keep | ||
| } | ||
Member There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. And this too. Member There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I find this a bit weird and possibly inconsistent, because I think this is the best solution: // on the top, addfunctionNOOP(){};// here:varlog=options.log!==false ? logerror : NOOP;// everywhere below, you can always count on `log` being a functionContributorAuthor There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. added the function and setup the logging properly. | ||
| if (typeof log !== 'function' && typeof log !== 'boolean') { | ||
| throw new TypeError('option log must be boolean'); | ||
Member There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If I am reading the code correctly, you are setting TBH, I don't see much value in this check, I'll simply remove it. | ||
| } | ||
Member There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. And this too. The flag | ||
| var safeFields = options.safeFields; | ||
| return function strongErrorHandler(err, req, res, next) { | ||
| // log the error | ||
| var str = stringify(err); | ||
| if (log) { | ||
| console.error('Unhandled error for request %s %s: %s', req.method, | ||
| req.path, err.stack); | ||
| } | ||
| // cannot actually respond | ||
| if (res._header) { | ||
| return req.socket.destroy(); | ||
| } | ||
| // delete err.stack if not debug mode | ||
| if (!debug) { | ||
| delete err.stack; | ||
| } | ||
| // respect err.statusCode | ||
| if (err.statusCode) { | ||
| res.statusCode = err.statusCode; | ||
| } | ||
| // respect err.status | ||
| if (err.status) { | ||
| res.statusCode = err.status; | ||
| } | ||
| // default status code to 500 | ||
| if (res.statusCode < 400) { | ||
Member There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What if | ||
| res.statusCode = 500; | ||
| } | ||
| if (res.statusCode == undefined) { | ||
| res.statusCode = 500; | ||
| } | ||
| // negotiate accepted types | ||
| var accept = accepts(req); | ||
| var type = accept.type('html', 'json', 'text'); | ||
Member There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Quoting from the spec: strongloop/loopback#1650 (comment)
I don't think you support that option yet. ContributorAuthor There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. not yet. I pointed the test case to the issue#4 | ||
| if (!accept) { | ||
| return 'Strong error Handler does not support the requested type. '; | ||
| } | ||
| // Security header for content sniffing | ||
| res.setHeader('X-Content-Type-Options', 'nosniff'); | ||
| // html | ||
| if (type === 'html') { | ||
| fs.readFile(__dirname + '/views/style.css', 'utf8', function(e, style) { | ||
| if (e) return next(e); | ||
| if (res.statusCode === 401) { | ||
| fs.readFile(__dirname + '/views/error-unauthorized.html', 'utf8', | ||
| function(e, html, style) { | ||
| return htmlTemplate(e, html, style); | ||
| }); | ||
| } else if (res.statusCode === 404) { | ||
| fs.readFile(__dirname + '/views/error-not-found.html', 'utf8', | ||
| function(e, html, style) { | ||
| return htmlTemplate(e, html, style); | ||
| }); | ||
| } else { | ||
| fs.readFile(__dirname + '/views/error.html', 'utf8', | ||
| function(e, html, style) { | ||
| return htmlTemplate(e, html, style); | ||
| }); | ||
| } | ||
| }); | ||
| function htmlTemplate(e, html, style) { | ||
| if (e) return next(e); | ||
| var isInspect = !err.stack && String(err) === toString.call(err); | ||
| var errorHtml = !isInspect ? | ||
| escapeHtmlBlock(str.split('\n', 1)[0] || 'Error') : | ||
| 'Error'; | ||
| var stack = !isInspect ? | ||
| String(str).split('\n').slice(1) : | ||
| [str]; | ||
| var stackHtml = stack | ||
| .map(function(v) { return '<li>' + escapeHtmlBlock(v) + '</li>'; }) | ||
| .join(''); | ||
| var body = html | ||
| .replace('{style}', style) | ||
| .replace('{stack}', stackHtml) | ||
| .replace('{title}', escapeHtml(exports.title)) | ||
| .replace('{statusCode}', res.statusCode) | ||
| .replace(/\{error\}/g, errorHtml); | ||
| res.setHeader('Content-Type', 'text/html; charset=utf-8'); | ||
| res.end(body); | ||
| } | ||
| // json | ||
| } else if (type === 'json') { | ||
| if (debug) { | ||
| var error = { name: err.name, message: err.message, stack: res.stack, | ||
| statusCode: res.statusCode, details: err.details }; | ||
| for (var prop in err) error[prop] = err[prop]; | ||
| var json = JSON.stringify({ error: error }); | ||
| res.setHeader('Content-Type', 'application/json; charset=utf-8'); | ||
| res.end(json); | ||
| return jsonSerializer; | ||
| } else if (!debug && res.statusCode >= 400) { | ||
| var error = { name: err.name, message: err.message, | ||
| statusCode: res.statusCode, details: err.details }; | ||
| for (var prop in err) error[prop] = err[prop]; | ||
| var json = JSON.stringify({ error: error }); | ||
| res.setHeader('Content-Type', 'application/json; charset=utf-8'); | ||
| res.end(json); | ||
| return jsonSerializer; | ||
| } else if (!debug && (res.statusCode == 500 || res.statusCode < 400 || | ||
| res.statusCode == undefined)) { | ||
| var error = { statusCode: res.statusCode, messahe: res.message }; | ||
| for (var prop in err) error[prop] = err[prop]; | ||
| var json = JSON.stringify({ error: error }); | ||
| res.setHeader('Content-Type', 'application/json; charset=utf-8'); | ||
| res.end(json); | ||
| return jsonSerializer; | ||
| } | ||
| // a custom JSON serializer function for producing JSON response bodies | ||
| // @param sanitizedData: response data containing only safe properties | ||
| // @param originalError: the original Error object | ||
| var jsonSerializer = function(sanitizedData, originalError) { | ||
Member There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. AFAICT, this is not used, please remove. | ||
| if (originalError.name === 'ValidationError') { | ||
| var details = sanitizedData.details || {}; | ||
| sanitizedData.issueCount = details.codes && | ||
| Object.keys(details.codes).length; | ||
| } | ||
| return sanitizedData; | ||
| }; | ||
| // plain text | ||
| } else { | ||
| res.setHeader('Content-Type', 'text/plain; charset=utf-8'); | ||
| res.end(str); | ||
| } | ||
| }; | ||
| }; | ||
| /** | ||
| * Escape a block of HTML, preserving whitespace. | ||
| * @api private | ||
| */ | ||
| function escapeHtmlBlock(str) { | ||
| return escapeHtml(str) | ||
| .replace(DOUBLE_SPACE_REGEXP, ' ') | ||
| .replace(NEW_LINE_GLOBAL_REGEXP, '<br>'); | ||
| } | ||
| /** | ||
| * Stringify a value. | ||
| * @api private | ||
| */ | ||
| function stringify(val) { | ||
| var stack = val.stack; | ||
| if (stack) { | ||
| return String(stack); | ||
| } | ||
| var str = String(val); | ||
| return str === toString.call(val) ? | ||
| inspect(val) : | ||
| str; | ||
| } | ||
| /** | ||
| * Log error to console. | ||
| * @api private | ||
| */ | ||
| function logerror(err, str) { | ||
| console.error(str); | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| { | ||
| "name": "strong-error-handler", | ||
| "description": "Strong Loop Error Handler in Production", | ||
| "version": "1.0.0", | ||
| "main": "index.js", | ||
| "scripts": { | ||
| "lint": "eslint .", | ||
| "test": "mocha -R spec --timeout 2000", | ||
| "posttest": "npm run lint" | ||
| }, | ||
| "dependencies": { | ||
| "accepts": "~1.3.0", | ||
| "escape-html": "~1.0.3", | ||
| "async": "^0.9.0", | ||
| "body-parser": "^1.12.0", | ||
| "cookie-parser": "^1.3.4", | ||
| "debug": "^2.1.2", | ||
| "loopback": "^2.8.0" | ||
| }, | ||
| "devDependencies": { | ||
| "after": "^0.8.1", | ||
| "chai": "^2.1.1", | ||
| "env-test": "^1.0.0", | ||
| "es5-shim": "^4.1.0", | ||
| "eslint": "^2.5.3", | ||
| "eslint-config-loopback": "^1.0.0", | ||
| "mocha": "^2.1.0", | ||
| "rc": "^1.0.0", | ||
| "sinon": "^1.15.4", | ||
| "supertest": "^1.1.0" | ||
| }, | ||
| "repository": { | ||
| "type": "git", | ||
| "url": "https://github.com/strongloop/strong-error-handler.git" | ||
| }, | ||
| "license": "MIT" | ||
| } |
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is it necessary to test so many different minor versions? I think this should be good enough:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
However, wouldn't we need 5 at least for the mean time as you said that in one of your previous comments? I am ok with 4 and 6 though.