{"action":"created","issue":{"url":"https://api.github.com/repos/NativeScript/NativeScript/issues/717","labels_url":"https://api.github.com/repos/NativeScript/NativeScript/issues/717/labels{/name}","comments_url":"https://api.github.com/repos/NativeScript/NativeScript/issues/717/comments","events_url":"https://api.github.com/repos/NativeScript/NativeScript/issues/717/events","html_url":"https://github.com/NativeScript/NativeScript/issues/717","id":106041456,"number":717,"title":"fetch() does not invoke catch() handlers on 400 responses","user":{"login":"tjvantoll","id":544280,"avatar_url":"https://avatars.githubusercontent.com/u/544280?v=3","gravatar_id":"","url":"https://api.github.com/users/tjvantoll","html_url":"https://github.com/tjvantoll","followers_url":"https://api.github.com/users/tjvantoll/followers","following_url":"https://api.github.com/users/tjvantoll/following{/other_user}","gists_url":"https://api.github.com/users/tjvantoll/gists{/gist_id}","starred_url":"https://api.github.com/users/tjvantoll/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/tjvantoll/subscriptions","organizations_url":"https://api.github.com/users/tjvantoll/orgs","repos_url":"https://api.github.com/users/tjvantoll/repos","events_url":"https://api.github.com/users/tjvantoll/events{/privacy}","received_events_url":"https://api.github.com/users/tjvantoll/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":2,"created_at":"2015-09-11T16:04:13Z","updated_at":"2015-09-11T21:28:52Z","closed_at":null,"body":"When upgrading the same to the 1.3 RC I ran into a little issue that took me a while to track down. It seems that the `fetch()` method does invoke `catch()` handlers when a backend returns a 400 response.\r\n\r\nHere's my example. I'm calling Telerik Backend Services to register a new user with the following code:\r\n\r\n```js\r\nfetch(config.apiUrl + \"Users\", {\r\n\tmethod: \"POST\",\r\n\tbody: JSON.stringify({\r\n\t\tUsername: viewModel.get(\"email\"),\r\n\t\tEmail: viewModel.get(\"email\"),\r\n\t\tPassword: viewModel.get(\"password\")\r\n\t}),\r\n\theaders: {\r\n\t\t\"Content-Type\": \"application/json\"\r\n\t}\r\n}).then(function(data) {\r\n\tresolve();\r\n}).catch(function(error) {\r\n\tconsole.log(error);\r\n\treject();\r\n});\r\n```\r\n\r\nIf I try to register a user that already exists in the backend, Telerik Backend Services returns the following JSON response:\r\n\r\n```\r\n{\"_bodyInit\":\"{\\\"message\\\":\\\"A user with the same username already exists.\\\",\\\"errorCode\\\":201}\",\"_bodyText\":\"{\\\"message\\\":\\\"A user with the same username already exists.\\\",\\\"errorCode\\\":201}\",\"type\":\"default\",\"url\":\"\",\"status\":400,\"ok\":false,\"statusText\":\"Bad Request\",\"headers\":{\"map\":{\"server\":[\"nginx\"],\"content-type\":[\"application/json; charset=utf-8\"],\"x-powered-by\":[\"Everlive\"],\"content-length\":[\"75\"],\"connection\":[\"keep-alive\"],\"date\":[\"Fri, 11 Sep 2015 15:58:38 GMT\"],\"access-control-allow-credentials\":[\"true\"]}}}\r\n```\r\n\r\nHowever, despite the response including `\"status\": 400`, I end up in the `then()` handler rather than the `catch()` handler. In the case of Groceries this means I show a confirmation dialog when registration fails, rather than correctly showing an error message. If you'd like to see this behavior for yourself I have a [1.3 branch of Groceries up](https://github.com/NativeScript/sample-Groceries/tree/1.3).\r\n\r\nUnless I'm missing something, which is quite possible, the current behavior seems wrong."},"comment":{"url":"https://api.github.com/repos/NativeScript/NativeScript/issues/comments/139665993","html_url":"https://github.com/NativeScript/NativeScript/issues/717#issuecomment-139665993","issue_url":"https://api.github.com/repos/NativeScript/NativeScript/issues/717","id":139665993,"user":{"login":"tjvantoll","id":544280,"avatar_url":"https://avatars.githubusercontent.com/u/544280?v=3","gravatar_id":"","url":"https://api.github.com/users/tjvantoll","html_url":"https://github.com/tjvantoll","followers_url":"https://api.github.com/users/tjvantoll/followers","following_url":"https://api.github.com/users/tjvantoll/following{/other_user}","gists_url":"https://api.github.com/users/tjvantoll/gists{/gist_id}","starred_url":"https://api.github.com/users/tjvantoll/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/tjvantoll/subscriptions","organizations_url":"https://api.github.com/users/tjvantoll/orgs","repos_url":"https://api.github.com/users/tjvantoll/repos","events_url":"https://api.github.com/users/tjvantoll/events{/privacy}","received_events_url":"https://api.github.com/users/tjvantoll/received_events","type":"User","site_admin":false},"created_at":"2015-09-11T21:28:52Z","updated_at":"2015-09-11T21:28:52Z","body":"Interesting. You're right, and actually `then()` is called regardless of the response code. I might need to write something about this. Thanks!"}}