2

I'm struggling to capture a HTTP response code correctly. In my test scenario I am uploading an image that is too large for my nginx configuration and it is throwing a 413 Http response but I cannot capture it using the following code:

... this.http.post( this.url, body, options )
        .map( res => {
            res.json().data;
        })
        .catch( ( error: Response ) => {

            if (error.status === 413) 
            {
                return Observable.throw('Asset to large');
            }
            else 
            {
                console.log( error );
                return Observable.throw( error.json().error || 'Server error' );
            }
        });
        .cache();

The output of the console log which it hits is always:

Response
    body:ProgressEvent
    headers:Headers
    ok:false
    status:0
    statusText:""
    type:3
    url:null

Can anybody please advise?

prime
  • 2,004
  • 3
  • 28
  • 45
  • So what's wrong with the Response object? – martin Nov 15 '16 at 14:15
  • It has a status of 0 – prime Nov 15 '16 at 14:16
  • So maybe the pb is with your backend, not frontend. – maxime1992 Nov 15 '16 at 14:35
  • @maxime Thanks for your feedback. Do you mean an issue with nginx? From what I can tell the server sent a correct response and I am trying to capture it. – prime Nov 15 '16 at 14:42
  • @prime take a look to : http://stackoverflow.com/a/19862540/2398593 Status 0 does not exists on HTTP so I assume YOU are returning this status instead of your 413 expected status. It means that you won't be able to handle the correct case on your front (unless you catch the error.status === 0 which doesn't seem right). – maxime1992 Nov 15 '16 at 14:50
  • @maxime Thankyou, looks like the right idea. – prime Nov 15 '16 at 14:55

0 Answers0