Throw Error() with $q promises

2,003 views
Skip to first unread message

ThomasBurleson

unread,
Jan 30, 2012, 4:02:06 PM1/30/12
to AngularJS
I thought I could throw an error in a $.q resultHandler... which would
then reject the promise?
e.g.

return @$http
.post( requestURL, requestData )
.then( ( result, statusCode, headers, config ) =>
response = result.data

# If statusCode > 0, then clear session and announce error
if response?.statusCode != 0
return throw Error( response.statusMessage )


Instead, it appears that I must do this:

@$q.reject( response.statusMessage )

So the question is: "Are exceptions supported to reject a promise ?"

Witold Szczerba

unread,
Jan 30, 2012, 4:28:02 PM1/30/12
to ang...@googlegroups.com
Hi,
as far as I can tell, you either "throw" or "return" something from
within a function. One cannot return a throw.
Try this instead:

throw response.statusMessage;

Regards,
Witold Szczerba

> --
> You received this message because you are subscribed to the Google Groups "AngularJS" group.
> To post to this group, send email to ang...@googlegroups.com.
> To unsubscribe from this group, send email to angular+u...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/angular?hl=en.
>

Vojta Jina

unread,
Jan 30, 2012, 6:25:50 PM1/30/12
to ang...@googlegroups.com
Hey Thomas,

If you throw, the exception is delegated to $exceptionHandler and then the promise is rejected. (that means if your $exceptionHandler needs to not rethrow the exception, otherwise it jumps off the whole js context).
As far as I remember, we did that, so that the exceptions don't get silently muted...

Igor might give you more details...

V.

ThomasBurleson

unread,
Jan 31, 2012, 9:53:52 AM1/31/12
to AngularJS
So `throw response.statusMessage` does indeed get captured by the
$exceptionHandler and reported on the console.

1) The docs should not that `throw new Error(response.statusMessage)`
does not work.

Also, a big issue here:

While the $exceptionHandler does capture the error, report it, and
maintain the js context... it does NOT reject
I thought the Q spec states that exceptions are intercepted and
reported as rejections!

- ThomasB

ThomasBurleson

unread,
Jan 31, 2012, 10:02:13 AM1/31/12
to AngularJS
Also, it appears that $q is acting funny (IMHO)

Before I used

`throw response.statusMessage` to reject

If instead, I use

`return $q.reject(response.statusMessage)`

then my error/reject handler is never called.

I would expect that a rejection in a resolveHandler would then fire
the reject handlers.

ThomasBurleson

unread,
Jan 31, 2012, 10:21:43 AM1/31/12
to AngularJS
My apologies!

After looking at the AngularJS source (line 7704 v0.10.6)
the reject or throw is captured/reported.

My error was attaching the errorHandler to the incorrect promise.

Thanks,
ThomasB

@Witold, Thanks for the tip regarding `throw response.statusMessage;`

On Jan 31, 8:53 am, ThomasBurleson <thomasburle...@gmail.com> wrote:

Witold Szczerba

unread,
Jan 31, 2012, 10:39:25 AM1/31/12
to ang...@googlegroups.com
This is why I always try using something like JsFiddle - and when it
still does not work, then I write an e-mail here with a link to my
case.

This is how I have figured out how does the "$q.reject" relates to
"throw". I had a major issue with the $q conception and I managed to
resolve it working on JsFiddle, but I was very close to writing an
e-mail here with a link to the problematic code snippet.

Regards,
Witold Szczerba

Vojta Jina

unread,
Jan 31, 2012, 12:24:45 PM1/31/12
to ang...@googlegroups.com
Glad the issue is resolved now :-D

V.

ThomasBurleson

unread,
Jan 31, 2012, 4:21:12 PM1/31/12
to AngularJS
Oy vey!
Can't believe you took the shot...

On Jan 31, 11:24 am, Vojta Jina <vojta.j...@gmail.com> wrote:
> Glad the issue is *resolved* now :-D
>
> V.
Reply all
Reply to author
Forward
0 new messages