fix(http): use Response for JSONP errors

Return Response when JSONP backend errors
This commit is contained in:
Rob Wormald
2015-10-29 17:50:12 -07:00
parent 69e4b62809
commit 31687efd64
2 changed files with 19 additions and 6 deletions

View File

@ -1,5 +1,5 @@
import {ConnectionBackend, Connection} from '../interfaces'; import {ConnectionBackend, Connection} from '../interfaces';
import {ReadyStates, RequestMethods} from '../enums'; import {ReadyStates, RequestMethods, ResponseTypes} from '../enums';
import {Request} from '../static_request'; import {Request} from '../static_request';
import {Response} from '../static_response'; import {Response} from '../static_response';
import {ResponseOptions, BaseResponseOptions} from '../base_response_options'; import {ResponseOptions, BaseResponseOptions} from '../base_response_options';
@ -11,6 +11,10 @@ import {StringWrapper, isPresent} from 'angular2/src/core/facade/lang';
// todo(robwormald): temporary until https://github.com/angular/angular/issues/4390 decided // todo(robwormald): temporary until https://github.com/angular/angular/issues/4390 decided
var Rx = require('@reactivex/rxjs/dist/cjs/Rx'); var Rx = require('@reactivex/rxjs/dist/cjs/Rx');
var {Observable} = Rx; var {Observable} = Rx;
const JSONP_ERR_NO_CALLBACK = 'JSONP injected script did not invoke callback.';
const JSONP_ERR_WRONG_METHOD = 'JSONP requests must use GET request method.';
export abstract class JSONPConnection implements Connection { export abstract class JSONPConnection implements Connection {
readyState: ReadyStates; readyState: ReadyStates;
request: Request; request: Request;
@ -28,7 +32,7 @@ export class JSONPConnection_ extends JSONPConnection {
private baseResponseOptions?: ResponseOptions) { private baseResponseOptions?: ResponseOptions) {
super(); super();
if (req.method !== RequestMethods.Get) { if (req.method !== RequestMethods.Get) {
throw makeTypeError("JSONP requests must use GET request method."); throw makeTypeError(JSONP_ERR_WRONG_METHOD);
} }
this.request = req; this.request = req;
this.response = new Observable(responseObserver => { this.response = new Observable(responseObserver => {
@ -56,7 +60,12 @@ export class JSONPConnection_ extends JSONPConnection {
this.readyState = ReadyStates.Done; this.readyState = ReadyStates.Done;
_dom.cleanup(script); _dom.cleanup(script);
if (!this._finished) { if (!this._finished) {
responseObserver.error(makeTypeError('JSONP injected script did not invoke callback.')); let responseOptions =
new ResponseOptions({body: JSONP_ERR_NO_CALLBACK, type: ResponseTypes.Error});
if (isPresent(baseResponseOptions)) {
responseOptions = baseResponseOptions.merge(responseOptions);
}
responseObserver.error(new Response(responseOptions));
return; return;
} }
@ -73,7 +82,11 @@ export class JSONPConnection_ extends JSONPConnection {
if (this.readyState === ReadyStates.Cancelled) return; if (this.readyState === ReadyStates.Cancelled) return;
this.readyState = ReadyStates.Done; this.readyState = ReadyStates.Done;
_dom.cleanup(script); _dom.cleanup(script);
responseObserver.error(error); let responseOptions = new ResponseOptions({body: error.message, type: ResponseTypes.Error});
if (isPresent(baseResponseOptions)) {
responseOptions = baseResponseOptions.merge(responseOptions);
}
responseObserver.error(new Response(responseOptions));
}; };
script.addEventListener('load', onLoad); script.addEventListener('load', onLoad);

View File

@ -134,7 +134,7 @@ export function main() {
async.done(); async.done();
}, },
err => { err => {
expect(StringWrapper.contains(err.message, 'did not invoke callback')).toBe(true); expect(err.text()).toEqual('JSONP injected script did not invoke callback.');
async.done(); async.done();
}); });
@ -150,7 +150,7 @@ export function main() {
async.done(); async.done();
}, },
err => { err => {
expect(err['message']).toBe('Oops!'); expect(err.text()).toBe('Oops!');
async.done(); async.done();
}); });