|
1 | 1 | /**
|
2 | 2 | * @author Jason Dobry <[email protected]>
|
3 | 3 | * @file js-data-http.js
|
4 |
| -* @version 1.0.0-alpha.1 - Homepage <http://www.js-data.iojs-data-http/> |
| 4 | +* @version 1.0.0-alpha.2 - Homepage <http://www.js-data.iojs-data-http/> |
5 | 5 | * @copyright (c) 2014 Jason Dobry
|
6 | 6 | * @license MIT <https://github.com/js-data/js-data-http/blob/master/LICENSE>
|
7 | 7 | *
|
@@ -1418,8 +1418,13 @@ defaultsPrototype.forceTrailingSlash = '';
|
1418 | 1418 |
|
1419 | 1419 | defaultsPrototype.httpConfig = {};
|
1420 | 1420 |
|
1421 |
| -defaultsPrototype.log = console ? function (a, b, c, d, e) { |
1422 |
| - console.log(a, b, c, d, e); |
| 1421 | +defaultsPrototype.log = console ? function (a, b) { |
| 1422 | + console[typeof console.info === 'function' ? 'info' : 'log'](a, b); |
| 1423 | +} : function () { |
| 1424 | +}; |
| 1425 | + |
| 1426 | +defaultsPrototype.error = console ? function (a, b) { |
| 1427 | + console[typeof console.error === 'function' ? 'error' : 'log'](a, b); |
1423 | 1428 | } : function () {
|
1424 | 1429 | };
|
1425 | 1430 |
|
@@ -1448,17 +1453,28 @@ dsHttpAdapterPrototype.getAllPath = function (resourceConfig, options) {
|
1448 | 1453 |
|
1449 | 1454 | dsHttpAdapterPrototype.HTTP = function (config) {
|
1450 | 1455 | var _this = this;
|
1451 |
| - var start = new Date().getTime(); |
| 1456 | + var start = new Date(); |
1452 | 1457 | config = deepMixIn(config, _this.defaults.httpConfig);
|
1453 | 1458 | if (_this.defaults.forceTrailingSlash && config.url[config.url.length] !== '/') {
|
1454 | 1459 | config.url += '/';
|
1455 | 1460 | }
|
1456 |
| - return http(config).then(function (data) { |
1457 |
| - if (_this.defaults.log) { |
1458 |
| - _this.defaults.log(data.config.method.toUpperCase() + ' request: ' + data.config.url + ' Time taken: ' + (new Date().getTime() - start) + 'ms', data); |
| 1461 | + |
| 1462 | + function logResponse(data) { |
| 1463 | + var str = start.toUTCString() + ' - ' + data.config.method.toUpperCase() + ' ' + data.config.url + ' - ' + data.status + ' ' + (new Date().getTime() - start.getTime()) + 'ms'; |
| 1464 | + if (data.status >= 200 && data.status < 300) { |
| 1465 | + if (_this.defaults.log) { |
| 1466 | + _this.defaults.log(str, data); |
| 1467 | + } |
| 1468 | + return data; |
| 1469 | + } else { |
| 1470 | + if (_this.defaults.error) { |
| 1471 | + _this.defaults.error('FAILED: ' + str, data); |
| 1472 | + } |
| 1473 | + throw data; |
1459 | 1474 | }
|
1460 |
| - return data; |
1461 |
| - }); |
| 1475 | + } |
| 1476 | + |
| 1477 | + return http(config).then(logResponse, logResponse); |
1462 | 1478 | };
|
1463 | 1479 |
|
1464 | 1480 | dsHttpAdapterPrototype.GET = function (url, config) {
|
|
0 commit comments