{"action":"created","issue":{"url":"https://api.github.com/repos/WordPress/gutenberg/issues/12375","repository_url":"https://api.github.com/repos/WordPress/gutenberg","labels_url":"https://api.github.com/repos/WordPress/gutenberg/issues/12375/labels{/name}","comments_url":"https://api.github.com/repos/WordPress/gutenberg/issues/12375/comments","events_url":"https://api.github.com/repos/WordPress/gutenberg/issues/12375/events","html_url":"https://github.com/WordPress/gutenberg/issues/12375","id":384984541,"node_id":"MDU6SXNzdWUzODQ5ODQ1NDE=","number":12375,"title":"redux-routine casts anything to an Error object from a rejected promise.","user":{"login":"TimothyBJacobs","id":3460448,"node_id":"MDQ6VXNlcjM0NjA0NDg=","avatar_url":"https://avatars2.githubusercontent.com/u/3460448?v=4","gravatar_id":"","url":"https://api.github.com/users/TimothyBJacobs","html_url":"https://github.com/TimothyBJacobs","followers_url":"https://api.github.com/users/TimothyBJacobs/followers","following_url":"https://api.github.com/users/TimothyBJacobs/following{/other_user}","gists_url":"https://api.github.com/users/TimothyBJacobs/gists{/gist_id}","starred_url":"https://api.github.com/users/TimothyBJacobs/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/TimothyBJacobs/subscriptions","organizations_url":"https://api.github.com/users/TimothyBJacobs/orgs","repos_url":"https://api.github.com/users/TimothyBJacobs/repos","events_url":"https://api.github.com/users/TimothyBJacobs/events{/privacy}","received_events_url":"https://api.github.com/users/TimothyBJacobs/received_events","type":"User","site_admin":false},"labels":[{"id":995368576,"node_id":"MDU6TGFiZWw5OTUzNjg1NzY=","url":"https://api.github.com/repos/WordPress/gutenberg/labels/[Package]%20API%20fetch","name":"[Package] API fetch","color":"ed2572","default":false},{"id":1017320900,"node_id":"MDU6TGFiZWwxMDE3MzIwOTAw","url":"https://api.github.com/repos/WordPress/gutenberg/labels/[Package]%20Redux%20Routine","name":"[Package] Redux Routine","color":"ed2572","default":false},{"id":531680823,"node_id":"MDU6TGFiZWw1MzE2ODA4MjM=","url":"https://api.github.com/repos/WordPress/gutenberg/labels/[Type]%20Bug","name":"[Type] Bug","color":"d93f0b","default":false}],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":1,"created_at":"2018-11-27T21:03:03Z","updated_at":"2018-11-27T21:58:29Z","closed_at":null,"author_association":"CONTRIBUTOR","body":"**Describe the bug**\r\nWhen an `apiFetch` is yielded from a control, it is impossible to access the response error object because it is cast to an `Error` object which only accepts strings. \r\n\r\n**To Reproduce**\r\nSteps to reproduce the behavior:\r\nSetup a `apiFetch` control. I'm using the definition that GB uses in `core-data`.\r\n\r\nIn an action or resolver, yield an `apiFetch` that server will return a non 200 error for.\r\n\r\n```js\r\nfunction* myAction() {\r\n  try {\r\n    yield apiFetch( /* ... */ );\r\n  } catch ( e ) {\r\n    console.dir( e );\r\n  }\r\n}\r\n```\r\n\r\ne is an `Error` object with the message `[object Object]`.\r\n\r\nThis seems to happen because `castError` in `redux-routine` will cast anything rejected from a promise to an `Error` object which only supports `string` messages.\r\n\r\nSee related Slack discussion: https://wordpress.slack.com/archives/C02QB2JS7/p1543350222369000\r\n\r\n**Expected behavior**\r\nTo be able to access the original REST Response body.\r\n\r\n**Workarounds**\r\nChange the control definition to:\r\n\r\n```\r\nAPI_FETCH( { request } ) {\r\n\treturn triggerApiFetch( request ).catch( ( error ) => {\r\n\t\tthrow new Error( JSON.stringify( error ) );\r\n\t} );\r\n},\r\n```\r\n\r\nThis would then require parsing the JSON again in any consuming code."},"comment":{"url":"https://api.github.com/repos/WordPress/gutenberg/issues/comments/442233071","html_url":"https://github.com/WordPress/gutenberg/issues/12375#issuecomment-442233071","issue_url":"https://api.github.com/repos/WordPress/gutenberg/issues/12375","id":442233071,"node_id":"MDEyOklzc3VlQ29tbWVudDQ0MjIzMzA3MQ==","user":{"login":"TimothyBJacobs","id":3460448,"node_id":"MDQ6VXNlcjM0NjA0NDg=","avatar_url":"https://avatars2.githubusercontent.com/u/3460448?v=4","gravatar_id":"","url":"https://api.github.com/users/TimothyBJacobs","html_url":"https://github.com/TimothyBJacobs","followers_url":"https://api.github.com/users/TimothyBJacobs/followers","following_url":"https://api.github.com/users/TimothyBJacobs/following{/other_user}","gists_url":"https://api.github.com/users/TimothyBJacobs/gists{/gist_id}","starred_url":"https://api.github.com/users/TimothyBJacobs/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/TimothyBJacobs/subscriptions","organizations_url":"https://api.github.com/users/TimothyBJacobs/orgs","repos_url":"https://api.github.com/users/TimothyBJacobs/repos","events_url":"https://api.github.com/users/TimothyBJacobs/events{/privacy}","received_events_url":"https://api.github.com/users/TimothyBJacobs/received_events","type":"User","site_admin":false},"created_at":"2018-11-27T21:58:29Z","updated_at":"2018-11-27T21:58:29Z","author_association":"CONTRIBUTOR","body":"I opened a PR that fixes this using the approach of throwing actual `Error` instances.\r\n\r\nAnother possibility is that we expose a `responseToError` function that creates the `ErrorResponse` object from a response object instead of always doing it. This would maintain 100% BC and then the control definition can be changed to:\r\n\r\n```js\r\nfunction responseToError( response ) {\r\n\tthrow new ErrorResponse( response );\r\n}\r\n```\r\n\r\n```js\r\nAPI_FETCH( { request } ) {\r\n\treturn triggerApiFetch( request ).catch( responseToError );\r\n},\r\n```\r\nThough I think this would end up giving us a _slightly_ less useful stack trace."}}
{"actor":{"display_login":"TimothyBJacobs"}}