{"id":6326,"date":"2018-06-24T19:18:00","date_gmt":"2018-06-24T19:18:00","guid":{"rendered":"http:\/\/putridparrot.com\/blog\/?p=6326"},"modified":"2018-06-24T19:18:00","modified_gmt":"2018-06-24T19:18:00","slug":"errors-within-grpc","status":"publish","type":"post","link":"https:\/\/putridparrot.com\/blog\/errors-within-grpc\/","title":{"rendered":"Errors within gRPC"},"content":{"rendered":"<p>Whilst we&#8217;d love our code to work perfectly, we also need to handle errors that might come from our server code. <\/p>\n<p>We might simple wrap any response&#8217;s in a Result class (or similar) which returns data along with a success or failure status code (or ofcourse include a status code in a response object). <\/p>\n<p>C#, Java etc. also use exceptions, so we might prefer to throw exceptions on the server.<\/p>\n<p>gRPC supports exceptions but with some limitations&#8230;<\/p>\n<p><strong>Exceptions come with status codes<\/strong><\/p>\n<p>Any exceptions thrown on the server will result in <em>RpcException<\/em> being thrown &#8211; these do NOT reflect the exception that was originally thrown, i.e. if your server code threw an <em>ArgumentException<\/em> the client will only ever see an <em>RpcExecption<\/em> with a StatusCode of Unknown. <\/p>\n<p>As you can see, an RpcException includes a status code, so obviously our server can instead throw an RpcException and supply the Status and StatusCode along with it, for example<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\nthrow new RpcException(\r\n   new Status(\r\n      StatusCode.FailedPrecondition, \r\n      $&quot;Argument {nameof(request)} is invalid&quot;));\r\n<\/pre>\n<p>Ofcourse handling such exceptions is as simple as<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\ntry\r\n{\r\n   var response = client.Query(new NotesReques())\r\n}\r\ncatch(RpcException e) \r\n{\r\n   Debug.WriteLine(e.Status.Detail);\r\n   Debug.WriteLine(e.Status.StatusCode);\r\n}\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Whilst we&#8217;d love our code to work perfectly, we also need to handle errors that might come from our server code. We might simple wrap any response&#8217;s in a Result class (or similar) which returns data along with a success or failure status code (or ofcourse include a status code in a response object). C#, [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[221],"tags":[],"class_list":["post-6326","post","type-post","status-publish","format-standard","hentry","category-grpc"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/6326","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/comments?post=6326"}],"version-history":[{"count":2,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/6326\/revisions"}],"predecessor-version":[{"id":6328,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/6326\/revisions\/6328"}],"wp:attachment":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/media?parent=6326"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/categories?post=6326"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/tags?post=6326"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}