Skip to content

Commit

Permalink
Merge pull request #53 from featurist/es5-check
Browse files Browse the repository at this point in the history
fix es5 compat issues, add es5 check
  • Loading branch information
refractalize authored Nov 13, 2019
2 parents 8825fb5 + 4c06adc commit 94484dd
Show file tree
Hide file tree
Showing 7 changed files with 641 additions and 58 deletions.
2 changes: 1 addition & 1 deletion browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ var xhr = require('./middleware/xhr')

module.exports = client([
require('./middleware/jsonp'),
require('./middleware/exception'),
require('./middleware/browserException'),
require('./middleware/formBrowser'),
require('./middleware/jsonBrowser'),
require('./middleware/textBrowser'),
Expand Down
2 changes: 1 addition & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ var http = require('./middleware/http')

module.exports = client([
require('./middleware/log'),
require('./middleware/exception'),
require('./middleware/serverException'),
require('./middleware/textServer'),
require('./middleware/formServer'),
require('./middleware/jsonServer'),
Expand Down
4 changes: 4 additions & 0 deletions middleware/browserException.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
var middleware = require('./middleware')
var exceptionMiddleware = require('./exception')()

module.exports = middleware('exception', exceptionMiddleware)
45 changes: 24 additions & 21 deletions middleware/exception.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,29 @@
var middleware = require('./middleware')
var extend = require('../extend')
var obfuscateUrlPassword = require('../obfuscateUrlPassword')
var logError = require('debug')('httpism:error')
var prepareForLogging = require('./prepareForLogging')
var logDetailError = require('debug')('httpism:response:error')

module.exports = middleware('exception', function (request, next) {
return next().then(function (response) {
var exceptions = request.options.exceptions
var isException = exceptions === false ? false : typeof exceptions === 'function' ? exceptions(response) : response.statusCode >= 400
module.exports = function (logError, logDetailError) {
return function (request, next) {
return next().then(function (response) {
var exceptions = request.options.exceptions
var isException = exceptions === false ? false : typeof exceptions === 'function' ? exceptions(response) : response.statusCode >= 400

if (isException) {
var obfuscatedUrl = obfuscateUrlPassword(request.url)
var msg = request.method.toUpperCase() + ' ' + obfuscatedUrl + ' => ' + response.statusCode + ' ' + response.statusText
logError(msg)
var logResponse = prepareForLogging(response)
logDetailError(logResponse)
var error = extend(new Error(msg), response)
error.url = obfuscatedUrl
throw error
} else {
return response
}
})
})
if (isException) {
var obfuscatedUrl = obfuscateUrlPassword(request.url)
var msg = request.method.toUpperCase() + ' ' + obfuscatedUrl + ' => ' + response.statusCode + ' ' + response.statusText
if (logError) {
logError(msg)
}
var logResponse = prepareForLogging(response)
if (logDetailError) {
logDetailError(logResponse)
}
var error = extend(new Error(msg), response)
error.url = obfuscatedUrl
throw error
} else {
return response
}
})
}
}
6 changes: 6 additions & 0 deletions middleware/serverException.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
var middleware = require('./middleware')
var logError = require('debug')('httpism:error')
var logDetailError = require('debug')('httpism:response:error')
var exceptionMiddleware = require('./exception')(logError, logDetailError)

module.exports = middleware('exception', exceptionMiddleware)
10 changes: 7 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
"cookie-parser": "1.4.4",
"cors": "2.8.5",
"electron": "6.0.12",
"es-check": "^5.0.0",
"es6-promise": "4.2.8",
"eslint": "6.5.1",
"eslint-config-standard": "14.1.0",
Expand Down Expand Up @@ -54,16 +55,19 @@
"uglify-js": "3.6.3",
"url-template": "2.0.8",
"watchify": "3.11.1",
"webpack": "4.41.2"
"webpack": "^4.41.2",
"webpack-cli": "^3.3.10"
},
"directories": {
"test": "test"
},
"scripts": {
"test": "npm run test-mocha && npm run test-browser -- --single-run --browsers Electron && eslint .",
"test": "npm run test-mocha && npm run test-browser -- --single-run --browsers Electron && eslint . && yarn test-es5",
"test-browser": "karma start",
"test-es5": "yarn webpack-build && es-check es5 _httpism.js && rm _httpism.js",
"test-mocha": "DEBUG=httpism* mocha",
"size": "webpack browser.js _httpism.js && uglifyjs --compress warnings=false --mangle -- _httpism.js > _httpism.min.js && gzip < _httpism.min.js > _httpism.min.js.gz && ls -lh _httpism.*"
"webpack-build": "webpack-cli browser.js --mode production --output _httpism.js",
"size": "yarn webpack-build && uglifyjs --compress --mangle -- _httpism.js > _httpism.min.js && gzip < _httpism.min.js > _httpism.min.js.gz && ls -lh _httpism.* && rm _httpism.*"
},
"repository": {
"type": "git",
Expand Down
Loading

0 comments on commit 94484dd

Please sign in to comment.