From 6362e92ba066c31434fb09d8ad61c0ac6e3408f5 Mon Sep 17 00:00:00 2001 From: Art Pai Date: Wed, 23 Sep 2015 00:49:18 +0800 Subject: [PATCH] ability to inject default errorHandler --- index.js | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/index.js b/index.js index 62ba4df..69724e4 100644 --- a/index.js +++ b/index.js @@ -11,7 +11,7 @@ return '?' + arr.join('&') } - function _fetch (method, url, opts, data, queryParams) { + function _fetch (method, url, opts, data, queryParams, errorHandler) { opts.method = method opts.headers = opts.headers || {} opts.responseAs = (opts.responseAs && ['json', 'text'].indexOf(opts.responseAs) >= 0) ? opts.responseAs : 'json' @@ -36,11 +36,16 @@ } var err = new Error(response.statusText) err.response = response + + if(errorHandler) { + errorHandler(response) + } + throw err }) } - function fetchival (url, opts) { + function fetchival (url, opts, errorHandler) { opts = opts || {} var _ = function (u, o) { @@ -52,23 +57,24 @@ } _.get = function (queryParams) { - return _fetch('GET', url, opts, null, queryParams) + return _fetch('GET', url, opts, null, queryParams, errorHandler) } _.post = function (data) { - return _fetch('POST', url, opts, data) + return _fetch('POST', url, opts, data, null, errorHandler) } _.put = function (data) { - return _fetch('PUT', url, opts, data) + console.log('datadatadatadatadata', data); + return _fetch('PUT', url, opts, data, null, errorHandler) } _.patch = function (data) { - return _fetch('PATCH', url, opts, data) + return _fetch('PATCH', url, opts, data, null, errorHandler) } _.delete = function () { - return _fetch('DELETE', url, opts) + return _fetch('DELETE', url, opts, null, null, errorHandler) } return _