text: (window.webpackJsonp=window.webpackJsonp||[]).push([[37],{142:function(e,n,t){"use strict";t.d(n,"a",(function(){return s})),t.d(n,"b",(function(){return u}));var a=t(0),r=t.n(a);function i(e,n,t){return n in e?Object.defineProperty(e,n,{value:t,enumerable:!0,configurable:!0,writable:!0}):e[n]=t,e}function c(e,n){var t=Object.keys(e);if(Object.getOwnPropertySymbols){var a=Object.getOwnPropertySymbols(e);n&&(a=a.filter((function(n){return Object.getOwnPropertyDescriptor(e,n).enumerable}))),t.push.apply(t,a)}return t}function o(e){for(var n=1;n<arguments.length;n++){var t=null!=arguments[n]?arguments[n]:{};n%2?c(Object(t),!0).forEach((function(n){i(e,n,t[n])})):Object.getOwnPropertyDescriptors?Object.defineProperties(e,Object.getOwnPropertyDescriptors(t)):c(Object(t)).forEach((function(n){Object.defineProperty(e,n,Object.getOwnPropertyDescriptor(t,n))}))}return e}function b(e,n){if(null==e)return{};var t,a,r=function(e,n){if(null==e)return{};var t,a,r={},i=Object.keys(e);for(a=0;a<i.length;a++)t=i[a],n.indexOf(t)>=0||(r[t]=e[t]);return r}(e,n);if(Object.getOwnPropertySymbols){var i=Object.getOwnPropertySymbols(e);for(a=0;a<i.length;a++)t=i[a],n.indexOf(t)>=0||Object.prototype.propertyIsEnumerable.call(e,t)&&(r[t]=e[t])}return r}var p=r.a.createContext({}),l=function(e){var n=r.a.useContext(p),t=n;return e&&(t="function"==typeof e?e(n):o(o({},n),e)),t},s=function(e){var n=l(e.components);return r.a.createElement(p.Provider,{value:n},e.children)},d={inlineCode:"code",wrapper:function(e){var n=e.children;return r.a.createElement(r.a.Fragment,{},n)}},m=r.a.forwardRef((function(e,n){var t=e.components,a=e.mdxType,i=e.originalType,c=e.parentName,p=b(e,["components","mdxType","originalType","parentName"]),s=l(t),m=a,u=s["".concat(c,".").concat(m)]||s[m]||d[m]||i;return t?r.a.createElement(u,o(o({ref:n},p),{},{components:t})):r.a.createElement(u,o({ref:n},p))}));function u(e,n){var t=arguments,a=n&&n.mdxType;if("string"==typeof e||a){var i=t.length,c=new Array(i);c[0]=m;var o={};for(var b in n)hasOwnProperty.call(n,b)&&(o[b]=n[b]);o.originalType=e,o.mdxType="string"==typeof e?e:a,c[1]=o;for(var p=2;p<i;p++)c[p]=t[p];return r.a.createElement.apply(null,c)}return r.a.createElement.apply(null,t)}m.displayName="MDXCreateElement"},143:function(e,n,t){"use strict";var a=t(0),r=t(19);n.a=function(){var e=Object(a.useContext)(r.a);if(null===e)throw new Error("Docusaurus context not provided");return e}},144:function(e,n,t){"use strict";t.d(n,"b",(function(){return i})),t.d(n,"a",(function(){return c}));var a=t(143),r=t(145);function i(){var e=Object(a.a)().siteConfig,n=(e=void 0===e?{}:e).baseUrl,t=void 0===n?"/":n,i=e.url;return{withBaseUrl:function(e,n){return function(e,n,t,a){var i=void 0===a?{}:a,c=i.forcePrependBaseUrl,o=void 0!==c&&c,b=i.absolute,p=void 0!==b&&b;if(!t)return t;if(t.startsWith("#"))return t;if(Object(r.b)(t))return t;if(o)return n+t;var l=t.startsWith(n)?t:n+t.replace(/^\//,"");return p?e+l:l}(i,t,e,n)}}}function c(e,n){return void 0===n&&(n={}),(0,i().withBaseUrl)(e,n)}},145:function(e,n,t){"use strict";function a(e){return!0===/^(\w*:|\/\/)/.test(e)}function r(e){return void 0!==e&&!a(e)}t.d(n,"b",(function(){return a})),t.d(n,"a",(function(){return r}))},93:function(e,n,t){"use strict";t.r(n),t.d(n,"frontMatter",(function(){return o})),t.d(n,"metadata",(function(){return b})),t.d(n,"rightToc",(function(){return p})),t.d(n,"default",(function(){return s}));var a=t(2),r=t(6),i=(t(0),t(142)),c=t(144),o={id:"friendly-exception",title:"7. \u53cb\u597d\u5f02\u5e38\u5904\u7406",sidebar_label:"7. \u53cb\u597d\u5f02\u5e38\u5904\u7406"},b={unversionedId:"friendly-exception",id:"friendly-exception",isDocsHomePage:!1,title:"7. \u53cb\u597d\u5f02\u5e38\u5904\u7406",description:"7.1 \u4ec0\u4e48\u662f\u5f02\u5e38",source:"@site/docs\\friendly-exception.mdx",slug:"/friendly-exception",permalink:"/fur/docs/friendly-exception",editUrl:"https://gitee.com/monksoul/Fur/tree/main/handbook/docs/friendly-exception.mdx",version:"current",sidebar_label:"7. \u53cb\u597d\u5f02\u5e38\u5904\u7406",sidebar:"docs",previous:{title:"6. \u89c4\u8303\u5316\u63a5\u53e3\u6587\u6863",permalink:"/fur/docs/specification-document"},next:{title:"8. \u6570\u636e\u6821\u9a8c",permalink:"/fur/docs/data-validation"}},p=[{value:"7.1 \u4ec0\u4e48\u662f\u5f02\u5e38",id:"71-\u4ec0\u4e48\u662f\u5f02\u5e38",children:[]},{value:"7.2 \u5904\u7406\u5f02\u5e38\u65b9\u5f0f",id:"72-\u5904\u7406\u5f02\u5e38\u65b9\u5f0f",children:[]},{value:"7.3 \u4ec0\u4e48\u662f\u53cb\u597d\u5f02\u5e38\u5904\u7406",id:"73-\u4ec0\u4e48\u662f\u53cb\u597d\u5f02\u5e38\u5904\u7406",children:[{value:"7.3.1 \u975e\u53cb\u597d\u5f02\u5e38\u5904\u7406",id:"731-\u975e\u53cb\u597d\u5f02\u5e38\u5904\u7406",children:[]},{value:"7.3.2 \u53cb\u597d\u5f02\u5e38\u5904\u7406",id:"732-\u53cb\u597d\u5f02\u5e38\u5904\u7406",children:[]}]},{value:"7.4 \u53cb\u597d\u5f02\u5e38\u5904\u7406\u4f7f\u7528\u793a\u4f8b",id:"74-\u53cb\u597d\u5f02\u5e38\u5904\u7406\u4f7f\u7528\u793a\u4f8b",children:[{value:"7.4.1 \u6ce8\u518c\u53cb\u597d\u5f02\u5e38\u670d\u52a1",id:"741-\u6ce8\u518c\u53cb\u597d\u5f02\u5e38\u670d\u52a1",children:[]},{value:"7.4.2 \u4e24\u4e2a\u4f8b\u5b50",id:"742-\u4e24\u4e2a\u4f8b\u5b50",children:[]}]},{value:"7.5 \u5173\u4e8e Oops.Oh",id:"75-\u5173\u4e8e-oopsoh",children:[{value:"7.6.1 \u4e3a\u4ec0\u4e48\u8d77\u8fd9\u4e2a\u540d\u5b57\uff1f",id:"761-\u4e3a\u4ec0\u4e48\u8d77\u8fd9\u4e2a\u540d\u5b57\uff1f",children:[]},{value:"7.6.2 <code>Oops.Oh</code> \u91cd\u8f7d\u65b9\u6cd5",id:"762-oopsoh-\u91cd\u8f7d\u65b9\u6cd5",children:[]}]},{value:"7.6 \u6700\u4f73\u5b9e\u8df5 \ud83e\udd17",id:"76-\u6700\u4f73\u5b9e\u8df5-",children:[{value:"7.6.1 \u521b\u5efa\u5f02\u5e38\u4fe1\u606f\u7c7b\u578b",id:"761-\u521b\u5efa\u5f02\u5e38\u4fe1\u606f\u7c7b\u578b",children:[]},{value:"7.6.2 \u5173\u4e8e <code>[ErrorCodeItemMetadata]</code>",id:"762-\u5173\u4e8e-errorcodeitemmetadata",children:[]},{value:"7.6.3 \u9759\u6001\u5f02\u5e38\u7c7b\u4f7f\u7528",id:"763-\u9759\u6001\u5f02\u5e38\u7c7b\u4f7f\u7528",children:[]},{value:"7.6.4 \u66f4\u591a\u4f8b\u5b50",id:"764-\u66f4\u591a\u4f8b\u5b50",children:[]}]},{value:"7.7 \u591a\u4e2a\u5f02\u5e38\u4fe1\u606f\u7c7b\u578b",id:"77-\u591a\u4e2a\u5f02\u5e38\u4fe1\u606f\u7c7b\u578b",children:[]},{value:"7.8 <code>IErrorCodeTypeProvider</code> \u63d0\u4f9b\u5668",id:"78-ierrorcodetypeprovider-\u63d0\u4f9b\u5668",children:[]},{value:"7.9 <code>appsetting.json</code> \u4e2d\u914d\u7f6e",id:"79-appsettingjson-\u4e2d\u914d\u7f6e",children:[]},{value:"7.10 <code>[IfException]</code> \u4f7f\u7528",id:"710-ifexception-\u4f7f\u7528",children:[{value:"7.10.1 \u4f7f\u7528\u793a\u4f8b",id:"7101-\u4f7f\u7528\u793a\u4f8b",children:[]},{value:"7.10.2 \u66f4\u591a\u4f8b\u5b50",id:"7102-\u66f4\u591a\u4f8b\u5b50",children:[]}]},{value:"7.11 \u5f02\u5e38\u6d88\u606f\u4f18\u5148\u7ea7",id:"711-\u5f02\u5e38\u6d88\u606f\u4f18\u5148\u7ea7",children:[]},{value:"7.12 \u591a\u8bed\u8a00\u652f\u6301",id:"712-\u591a\u8bed\u8a00\u652f\u6301",children:[]},{value:"7.13 \u5f02\u5e38\u6a21\u578b\u63d0\u4f9b\u5668",id:"713-\u5f02\u5e38\u6a21\u578b\u63d0\u4f9b\u5668",children:[]},{value:"7.14 \u5168\u5c40\u5f02\u5e38\u5904\u7406\u63d0\u4f9b\u5668",id:"714-\u5168\u5c40\u5f02\u5e38\u5904\u7406\u63d0\u4f9b\u5668",children:[]},{value:"7.15 \u53cd\u9988\u4e0e\u5efa\u8bae",id:"715-\u53cd\u9988\u4e0e\u5efa\u8bae",children:[]}],l={rightToc:p};function s(e){var n=e.components,t=Object(r.a)(e,["components"]);return Object(i.b)("wrapper",Object(a.a)({},l,t,{components:n,mdxType:"MDXLayout"}),Object(i.b)("h2",{id:"71-\u4ec0\u4e48\u662f\u5f02\u5e38"},"7.1 \u4ec0\u4e48\u662f\u5f02\u5e38"),Object(i.b)("p",null,"\u5f02\u5e38\u4e00\u822c\u662f\u6307\u8fd0\u884c\u671f\uff08\u6b64\u5904\u7279\u6307 ",Object(i.b)("inlineCode",{parentName:"p"},"Exception")," \u7c7b\uff09\u4f1a\u53d1\u751f\u7684\u5bfc\u81f4\u7a0b\u5e8f\u610f\u5916\u4e2d\u6b62\u7684\u95ee\u9898\uff0c\u662f\u4e00\u79cd\u5bf9\u95ee\u9898\u7684\u63cf\u8ff0\u540e\u7684\u5c01\u88c5\u5bf9\u8c61\u3002"),Object(i.b)("p",null,"\u5728\u8fc7\u53bb\u5f00\u53d1\u4e2d\uff0c\u901a\u5e38\u5f02\u5e38\u7531\u7cfb\u7edf\u8fd0\u884c\u65f6\u51fa\u9519\u629b\u51fa\uff0c\u4f46\u73b0\u5728\u7684\u5f00\u53d1\u8fc7\u7a0b\u4e2d\uff0c\u6211\u4eec\u5e94\u5728\u7a0b\u5e8f\u5f00\u53d1\u4e2d\u5408\u7406\u7684\u629b\u51fa\u5f02\u5e38\uff0c\u6bd4\u5982\u66f4\u65b0\u4e00\u6761\u4e0d\u5b58\u5728\u7684\u5b9e\u4f53\uff0c\u6216\u67e5\u8be2\u4e00\u4e2a\u4e0d\u5b58\u5728\u7684\u6570\u636e\u7b49\u7b49\u3002"),Object(i.b)("h2",{id:"72-\u5904\u7406\u5f02\u5e38\u65b9\u5f0f"},"7.2 \u5904\u7406\u5f02\u5e38\u65b9\u5f0f"),Object(i.b)("ul",null,Object(i.b)("li",{parentName:"ul"},"\u4e0d\u5904\u7406\uff0c\u76f4\u63a5\u4e2d\u65ad\u7a0b\u5e8f\u6267\u884c\uff08\u4e0d\u63a8\u8350\uff09"),Object(i.b)("li",{parentName:"ul"},"\u901a\u8fc7 ",Object(i.b)("inlineCode",{parentName:"li"},"try catch finally")," \u5904\u7406\uff08\u4e0d\u63a8\u8350\uff09"),Object(i.b)("li",{parentName:"ul"},"\u5168\u5c40\u7edf\u4e00\u5904\u7406\uff0c\u5e76\u8bb0\u5f55\u5f02\u5e38\u4fe1\u606f",Object(i.b)("strong",{parentName:"li"},"\uff08\u63a8\u8350\uff09")),Object(i.b)("li",{parentName:"ul"},"\u5f02\u5e38\u6ce8\u89e3\u65b9\u5f0f\u5904\u7406\uff0c\u652f\u6301",Object(i.b)("strong",{parentName:"li"},"\u672c\u5730\u5316")," ",Object(i.b)("strong",{parentName:"li"},"\uff08\u63a8\u8350\uff09"))),Object(i.b)("h2",{id:"73-\u4ec0\u4e48\u662f\u53cb\u597d\u5f02\u5e38\u5904\u7406"},"7.3 \u4ec0\u4e48\u662f\u53cb\u597d\u5f02\u5e38\u5904\u7406"),Object(i.b)("h3",{id:"731-\u975e\u53cb\u597d\u5f02\u5e38\u5904\u7406"},"7.3.1 \u975e\u53cb\u597d\u5f02\u5e38\u5904\u7406"),Object(i.b)("p",null,"\u5728\u4e86\u89e3\u53cb\u597d\u5f02\u5e38\u5904\u7406\u4e4b\u524d\u53ef\u4ee5\u770b\u770b\u975e\u53cb\u597d\u5f02\u5e38\u5904\u7406\uff1a"),Object(i.b)("ul",null,Object(i.b)("li",{parentName:"ul"},"\u5bf9\u7ec8\u7aef\u7528\u6237\u629b\u51fa ",Object(i.b)("inlineCode",{parentName:"li"},"500\u72b6\u6001\u7801")," \u5806\u6808\u4fe1\u606f"),Object(i.b)("li",{parentName:"ul"},"\u5927\u91cf\u7684 ",Object(i.b)("inlineCode",{parentName:"li"},"try catch")," \u4ee3\u7801\uff0c\u6c61\u67d3\u6b63\u5e38\u4e1a\u52a1\u903b\u8f91"),Object(i.b)("li",{parentName:"ul"},"\u6ca1\u6709\u89c4\u8303\u5316\u7684\u5f02\u5e38\u72b6\u6001\u7801\u548c\u5f02\u5e38\u6d88\u606f\u7ba1\u7406"),Object(i.b)("li",{parentName:"ul"},"\u6ca1\u6709\u5f02\u5e38\u65e5\u5fd7\u6536\u96c6\u8bb0\u5f55"),Object(i.b)("li",{parentName:"ul"},"\u4e0d\u652f\u6301\u5f02\u5e38\u6d88\u606f\u672c\u5730\u5316\u5904\u7406"),Object(i.b)("li",{parentName:"ul"},"\u4e0d\u652f\u6301\u5f02\u5e38\u7b56\u7565\uff0c\u5931\u8d25\u540e\u7a0b\u5e8f\u7acb\u5373\u7ec8\u6b62"),Object(i.b)("li",{parentName:"ul"},"\u4e0d\u652f\u6301\u5206\u5e03\u5f0f\u4e8b\u52a1 CAP"),Object(i.b)("li",{parentName:"ul"},"\u4e0d\u652f\u6301\u5f02\u5e38\u4f20\u64ad"),Object(i.b)("li",{parentName:"ul"},"\u8fd4\u56de\u7684\u5f02\u5e38\u683c\u5f0f\u6742\u4e71")),Object(i.b)("h3",{id:"732-\u53cb\u597d\u5f02\u5e38\u5904\u7406"},"7.3.2 \u53cb\u597d\u5f02\u5e38\u5904\u7406"),Object(i.b)("ul",null,Object(i.b)("li",{parentName:"ul"},"\u5bf9\u7ec8\u7aef\u7528\u6237\u63d0\u793a\u53cb\u597d"),Object(i.b)("li",{parentName:"ul"},"\u5bf9\u540e\u7aef\u5f00\u53d1\u4eba\u5458\u63d0\u4f9b\u8be6\u7ec6\u7684\u5f02\u5e38\u5806\u6808"),Object(i.b)("li",{parentName:"ul"},"\u4e0d\u5e72\u6270\u6b63\u5e38\u4e1a\u52a1\u903b\u8f91\u4ee3\u7801\uff0c\u5982 \u6ca1\u6709 ",Object(i.b)("inlineCode",{parentName:"li"},"try catch")," \u4ee3\u7801"),Object(i.b)("li",{parentName:"ul"},"\u652f\u6301\u5f02\u5e38\u72b6\u6001\u7801\u591a\u65b9\u8bbe\u7f6e"),Object(i.b)("li",{parentName:"ul"},"\u652f\u6301\u5f02\u5e38\u6d88\u606f\u672c\u5730\u5316"),Object(i.b)("li",{parentName:"ul"},"\u5f02\u5e38\u4fe1\u606f\u7edf\u4e00\u914d\u7f6e\u7ba1\u7406"),Object(i.b)("li",{parentName:"ul"},"\u652f\u6301\u5f02\u5e38\u7b56\u7565\uff0c\u5982\u91cd\u8bd5"),Object(i.b)("li",{parentName:"ul"},"\u652f\u6301\u5f02\u5e38\u65e5\u5fd7\u6536\u96c6\u8bb0\u5f55"),Object(i.b)("li",{parentName:"ul"},"\u652f\u6301 CAP \u5206\u5e03\u5f0f\u4e8b\u52a1\u5173\u8054"),Object(i.b)("li",{parentName:"ul"},"\u652f\u6301\u5185\u90e8\u5f02\u5e38\u5916\u90e8\u4f20\u64ad"),Object(i.b)("li",{parentName:"ul"},"\u652f\u6301\u8fd4\u56de\u7edf\u4e00\u7684\u5f02\u5e38\u683c\u5f0f\u6570\u636e")),Object(i.b)("h2",{id:"74-\u53cb\u597d\u5f02\u5e38\u5904\u7406\u4f7f\u7528\u793a\u4f8b"},"7.4 \u53cb\u597d\u5f02\u5e38\u5904\u7406\u4f7f\u7528\u793a\u4f8b"),Object(i.b)("p",null,Object(i.b)("inlineCode",{parentName:"p"},"Fur")," \u6846\u67b6\u63d0\u4f9b\u4e86\u975e\u5e38\u7075\u6d3b\u7684\u53cb\u597d\u5f02\u5e38\u5904\u7406\u65b9\u5f0f\u3002"),Object(i.b)("div",{className:"admonition admonition-important alert alert--info"},Object(i.b)("div",Object(a.a)({parentName:"div"},{className:"admonition-heading"}),Object(i.b)("h5",{parentName:"div"},Object(i.b)("span",Object(a.a)({parentName:"h5"},{className:"admonition-icon"}),Object(i.b)("svg",Object(a.a)({parentName:"span"},{xmlns:"http://www.w3.org/2000/svg",width:"14",height:"16",viewBox:"0 0 14 16"}),Object(i.b)("path",Object(a.a)({parentName:"svg"},{fillRule:"evenodd",d:"M7 2.3c3.14 0 5.7 2.56 5.7 5.7s-2.56 5.7-5.7 5.7A5.71 5.71 0 0 1 1.3 8c0-3.14 2.56-5.7 5.7-5.7zM7 1C3.14 1 0 4.14 0 8s3.14 7 7 7 7-3.14 7-7-3.14-7-7-7zm1 3H6v5h2V4zm0 6H6v2h2v-2z"})))),"\u5907\u6ce8")),Object(i.b)("div",Object(a.a)({parentName:"div"},{className:"admonition-content"}),Object(i.b)("p",{parentName:"div"},Object(i.b)("inlineCode",{parentName:"p"},".AddFriendlyException()")," \u9ed8\u8ba4\u5df2\u7ecf\u7ee7\u627f\u5728 ",Object(i.b)("inlineCode",{parentName:"p"},"AddInject()")," \u4e2d\u4e86\uff0c\u65e0\u9700\u518d\u6b21\u6ce8\u518c\u3002\u4e5f\u5c31\u662f ",Object(i.b)("inlineCode",{parentName:"p"},"7.4.1")," \u7ae0\u8282\u53ef\u4e0d\u914d\u7f6e\u3002"))),Object(i.b)("h3",{id:"741-\u6ce8\u518c\u53cb\u597d\u5f02\u5e38\u670d\u52a1"},"7.4.1 \u6ce8\u518c\u53cb\u597d\u5f02\u5e38\u670d\u52a1"),Object(i.b)("pre",null,Object(i.b)("code",Object(a.a)({parentName:"pre"},{className:"language-cs",metastring:'{11} title="Fur.Web.Core\\FurWebCoreStartup.cs"',"{11}":!0,title:'"Fur.Web.Core\\FurWebCoreStartup.cs"'}),"using Microsoft.Extensions.DependencyInjection;\n\nnamespace Fur.Web.Core\n{\n    [AppStartup(800)]\n    public sealed class FurWebCoreStartup : AppStartup\n    {\n        public void ConfigureServices(IServiceCollection services)\n        {\n            services.AddControllers()\n                    .AddFriendlyException();\n        }\n    }\n}\n")),Object(i.b)("div",{className:"admonition admonition-important alert alert--info"},Object(i.b)("div",Object(a.a)({parentName:"div"},{className:"admonition-heading"}),Object(i.b)("h5",{parentName:"div"},Object(i.b)("span",Object(a.a)({parentName:"h5"},{className:"admonition-icon"}),Object(i.b)("svg",Object(a.a)({parentName:"span"},{xmlns:"http://www.w3.org/2000/svg",width:"14",height:"16",viewBox:"0 0 14 16"}),Object(i.b)("path",Object(a.a)({parentName:"svg"},{fillRule:"evenodd",d:"M7 2.3c3.14 0 5.7 2.56 5.7 5.7s-2.56 5.7-5.7 5.7A5.71 5.71 0 0 1 1.3 8c0-3.14 2.56-5.7 5.7-5.7zM7 1C3.14 1 0 4.14 0 8s3.14 7 7 7 7-3.14 7-7-3.14-7-7-7zm1 3H6v5h2V4zm0 6H6v2h2v-2z"})))),"\u7279\u522b\u6ce8\u610f")),Object(i.b)("div",Object(a.a)({parentName:"div"},{className:"admonition-content"}),Object(i.b)("p",{parentName:"div"},Object(i.b)("inlineCode",{parentName:"p"},".AddFriendlyException()")," \u9700\u5728 ",Object(i.b)("inlineCode",{parentName:"p"},"services.AddControllers()")," \u4e4b\u540e\u6ce8\u518c\u3002"))),Object(i.b)("h3",{id:"742-\u4e24\u4e2a\u4f8b\u5b50"},"7.4.2 \u4e24\u4e2a\u4f8b\u5b50"),Object(i.b)("h4",{id:"\u7b80\u5355\u629b\u4e2a\u5f02\u5e38"},"\u7b80\u5355\u629b\u4e2a\u5f02\u5e38"),Object(i.b)("pre",null,Object(i.b)("code",Object(a.a)({parentName:"pre"},{className:"language-cs",metastring:"{2,12}","{2,12}":!0}),'using Fur.DynamicApiController;\nusing Fur.FriendlyException;\n\nnamespace Fur.Application\n{\n    public class FurAppService : IDynamicApiController\n    {\n        public int Get(int id)\n        {\n            if (id < 3)\n            {\n                throw Oops.Oh($"{id} \u4e0d\u80fd\u5c0f\u4e8e3");\n            }\n\n            return id;\n        }\n    }\n}\n')),Object(i.b)("p",null,"\u5982\u4e0b\u56fe\u6240\u793a\uff1a"),Object(i.b)("img",{src:Object(c.a)("img/yhyc1.gif")}),Object(i.b)("h4",{id:"\u629b\u51fa\u7279\u5b9a\u7c7b\u578b\u5f02\u5e38"},"\u629b\u51fa\u7279\u5b9a\u7c7b\u578b\u5f02\u5e38"),Object(i.b)("pre",null,Object(i.b)("code",Object(a.a)({parentName:"pre"},{className:"language-cs",metastring:"{2,13}","{2,13}":!0}),'using Fur.DynamicApiController;\nusing Fur.FriendlyException;\nusing System;\n\nnamespace Fur.Application\n{\n    public class FurAppService : IDynamicApiController\n    {\n        public int Get(int id)\n        {\n            if (id < 3)\n            {\n                throw Oops.Oh($"{id} \u4e0d\u80fd\u5c0f\u4e8e3\u3002", typeof(InvalidOperationException));\n            }\n\n            return id;\n        }\n    }\n}\n')),Object(i.b)("p",null,"\u5982\u4e0b\u56fe\u6240\u793a\uff1a"),Object(i.b)("img",{src:Object(c.a)("img/yhyc2.png")}),Object(i.b)("h2",{id:"75-\u5173\u4e8e-oopsoh"},"7.5 \u5173\u4e8e Oops.Oh"),Object(i.b)("p",null,"\u901a\u8fc7\u4e0a\u9762\u7684\u4f8b\u5b50\u53ef\u4ee5\u770b\u51fa\uff0c",Object(i.b)("inlineCode",{parentName:"p"},"Oops.Oh(errorMessage)")," \u53ef\u4ee5\u7ed3\u5408 ",Object(i.b)("inlineCode",{parentName:"p"},"throw")," \u629b\u51fa\u5f02\u5e38\u3002\u5bf9\u4e8e\u719f\u6089",Object(i.b)("inlineCode",{parentName:"p"},"C#"),"\u7684\u4eba\u5458\u6765\u8bf4\uff0c",Object(i.b)("inlineCode",{parentName:"p"},"throw")," \u540e\u9762\u53ea\u80fd ",Object(i.b)("inlineCode",{parentName:"p"},"Exception")," \u5b9e\u4f8b\u3002",Object(i.b)("inlineCode",{parentName:"p"},"Oops.Oh(...)")," \u65b9\u6cd5\u8fd4\u56de\u6b63\u662f ",Object(i.b)("inlineCode",{parentName:"p"},"Exception")," \u5b9e\u4f8b\u3002"),Object(i.b)("h3",{id:"761-\u4e3a\u4ec0\u4e48\u8d77\u8fd9\u4e2a\u540d\u5b57\uff1f"},"7.6.1 \u4e3a\u4ec0\u4e48\u8d77\u8fd9\u4e2a\u540d\u5b57\uff1f"),Object(i.b)("p",null,"\u8fd9\u4e2a\u540d\u5b57\u6765\u6e90\u4e8e\u4e00\u4e2a\u82f1\u8bed\u53e5\u5b50\uff1a",Object(i.b)("inlineCode",{parentName:"p"},"Oh, Oops!"),"\uff0c\u610f\u601d\u662f ",Object(i.b)("strong",{parentName:"p"},"\u5662\uff08\u54ce\uff09\uff0c\u51fa\u9519\u4e86\uff01"),"\uff0c\u6240\u4ee5\u5c31\u6709\u4e86 ",Object(i.b)("inlineCode",{parentName:"p"},"Oops.Oh"),"\u3002"),Object(i.b)("h3",{id:"762-oopsoh-\u91cd\u8f7d\u65b9\u6cd5"},"7.6.2 ",Object(i.b)("inlineCode",{parentName:"h3"},"Oops.Oh")," \u91cd\u8f7d\u65b9\u6cd5"),Object(i.b)("pre",null,Object(i.b)("code",Object(a.a)({parentName:"pre"},{className:"language-cs",metastring:"{13,22,30,39}","{13,22,30,39}":!0}),'using System;\n\nnamespace Fur.FriendlyException\n{\n    public static class Oops\n    {\n        /// <summary>\n        /// \u629b\u51fa\u5b57\u7b26\u4e32\u5f02\u5e38\n        /// </summary>\n        /// <param name="errorMessage">\u5f02\u5e38\u6d88\u606f</param>\n        /// <param name="args">String.Format \u53c2\u6570</param>\n        /// <returns>\u5f02\u5e38\u5b9e\u4f8b</returns>\n        public static Exception Oh(string errorMessage, params object[] args);\n\n        /// <summary>\n        /// \u629b\u51fa\u5b57\u7b26\u4e32\u5f02\u5e38\n        /// </summary>\n        /// <param name="errorMessage">\u5f02\u5e38\u6d88\u606f</param>\n        /// <param name="exceptionType">\u5177\u4f53\u5f02\u5e38\u7c7b\u578b</param>\n        /// <param name="args">String.Format \u53c2\u6570</param>\n        /// <returns>\u5f02\u5e38\u5b9e\u4f8b</returns>\n        public static Exception Oh(string errorMessage, Type exceptionType, params object[] args);\n\n        /// <summary>\n        /// \u629b\u51fa\u9519\u8bef\u7801\u5f02\u5e38\n        /// </summary>\n        /// <param name="errorCode">\u9519\u8bef\u7801</param>\n        /// <param name="args">String.Format \u53c2\u6570</param>\n        /// <returns>\u5f02\u5e38\u5b9e\u4f8b</returns>\n        public static Exception Oh(object errorCode, params object[] args);\n\n        /// <summary>\n        /// \u629b\u51fa\u9519\u8bef\u7801\u5f02\u5e38\n        /// </summary>\n        /// <param name="errorCode">\u9519\u8bef\u7801</param>\n        /// <param name="exceptionType">\u5177\u4f53\u5f02\u5e38\u7c7b\u578b</param>\n        /// <param name="args">String.Format \u53c2\u6570</param>\n        /// <returns>\u5f02\u5e38\u5b9e\u4f8b</returns>\n        public static Exception Oh(object errorCode, Type exceptionType, params object[] args);\n    }\n}\n')),Object(i.b)("h2",{id:"76-\u6700\u4f73\u5b9e\u8df5-"},"7.6 \u6700\u4f73\u5b9e\u8df5 \ud83e\udd17"),Object(i.b)("p",null,"\u5728 ",Object(i.b)("inlineCode",{parentName:"p"},"Fur")," \u6846\u67b6\u4e2d\uff0c\u63d0\u4f9b\u4e86\u975e\u5e38\u7075\u6d3b\u4e14\u89c4\u8303\u5316\u7684\u53cb\u597d\u5f02\u5e38\u5904\u7406\u65b9\u5f0f\uff0c\u901a\u8fc7\u8fd9\u4e2a\u65b9\u5f0f\u53ef\u4ee5\u65b9\u4fbf\u7ba1\u7406\u5f02\u5e38\u72b6\u6001\u7801\u3001\u5f02\u5e38\u4fe1\u606f\u53ca\u5f02\u5e38\u672c\u5730\u5316\u3002"),Object(i.b)("h3",{id:"761-\u521b\u5efa\u5f02\u5e38\u4fe1\u606f\u7c7b\u578b"},"7.6.1 \u521b\u5efa\u5f02\u5e38\u4fe1\u606f\u7c7b\u578b"),Object(i.b)("p",null,"\u5b9e\u73b0\u81ea\u5b9a\u4e49\u5f02\u5e38\u4fe1\u606f\u7c7b\u578b\u5fc5\u987b\u9075\u5faa\u4ee5\u4e0b\u914d\u7f6e\uff1a"),Object(i.b)("ul",null,Object(i.b)("li",{parentName:"ul"},"\u7c7b\u578b\u5fc5\u987b\u662f\u516c\u5f00\u4e14\u65f6 ",Object(i.b)("inlineCode",{parentName:"li"},"Enum")," \u679a\u4e3e\u7c7b\u578b"),Object(i.b)("li",{parentName:"ul"},"\u679a\u4e3e\u7c7b\u578b\u5fc5\u987b\u8d34\u6709 ",Object(i.b)("inlineCode",{parentName:"li"},"[ErrorCodeType]")," \u7279\u6027"),Object(i.b)("li",{parentName:"ul"},"\u679a\u4e3e\u4e2d\u6bcf\u4e00\u9879\u5fc5\u987b\u8d34\u6709 ",Object(i.b)("inlineCode",{parentName:"li"},"[ErrorCodeItemMetadata]")," \u7279\u6027")),Object(i.b)("pre",null,Object(i.b)("code",Object(a.a)({parentName:"pre"},{className:"language-cs",metastring:"{1,5,8,11,14,17}","{1,5,8,11,14,17}":!0}),'using Fur.FriendlyException;\n\nnamespace Fur.Application\n{\n    [ErrorCodeType]\n    public enum ErrorCodes\n    {\n        [ErrorCodeItemMetadata("{0} \u4e0d\u80fd\u5c0f\u4e8e {1}")]\n        z1000,\n\n        [ErrorCodeItemMetadata("\u6570\u636e\u4e0d\u5b58\u5728")]\n        x1000,\n\n        [ErrorCodeItemMetadata("{0} \u53d1\u73b0 {1} \u4e2a\u5f02\u5e38", "\u767e\u5c0f\u50e7", 2)]\n        x1001,\n\n        [ErrorCodeItemMetadata("\u670d\u52a1\u5668\u8fd0\u884c\u5f02\u5e38", ErrorCode = "Error")]\n        SERVER_ERROR\n    }\n}\n')),Object(i.b)("div",{className:"admonition admonition-important alert alert--info"},Object(i.b)("div",Object(a.a)({parentName:"div"},{className:"admonition-heading"}),Object(i.b)("h5",{parentName:"div"},Object(i.b)("span",Object(a.a)({parentName:"h5"},{className:"admonition-icon"}),Object(i.b)("svg",Object(a.a)({parentName:"span"},{xmlns:"http://www.w3.org/2000/svg",width:"14",height:"16",viewBox:"0 0 14 16"}),Object(i.b)("path",Object(a.a)({parentName:"svg"},{fillRule:"evenodd",d:"M7 2.3c3.14 0 5.7 2.56 5.7 5.7s-2.56 5.7-5.7 5.7A5.71 5.71 0 0 1 1.3 8c0-3.14 2.56-5.7 5.7-5.7zM7 1C3.14 1 0 4.14 0 8s3.14 7 7 7 7-3.14 7-7-3.14-7-7-7zm1 3H6v5h2V4zm0 6H6v2h2v-2z"})))),"important")),Object(i.b)("div",Object(a.a)({parentName:"div"},{className:"admonition-content"}),Object(i.b)("p",{parentName:"div"},Object(i.b)("inlineCode",{parentName:"p"},"Fur")," \u6846\u67b6\u63d0\u4f9b\u4e86 ",Object(i.b)("inlineCode",{parentName:"p"},"[ErrorCodeType]")," \u7279\u6027\u548c ",Object(i.b)("inlineCode",{parentName:"p"},"IErrorCodeTypeProvider")," \u63d0\u4f9b\u5668\u63a5\u53e3\u6765\u63d0\u4f9b\u5f02\u5e38\u4fe1\u606f\u626b\u63cf\uff0c\u8fd9\u91cc\u7528\u7684\u662f ",Object(i.b)("inlineCode",{parentName:"p"},"[ErrorCodeType]")," \u7279\u6027\u7c7b\u3002"))),Object(i.b)("h3",{id:"762-\u5173\u4e8e-errorcodeitemmetadata"},"7.6.2 \u5173\u4e8e ",Object(i.b)("inlineCode",{parentName:"h3"},"[ErrorCodeItemMetadata]")),Object(i.b)("p",null,Object(i.b)("inlineCode",{parentName:"p"},"Fur")," \u6846\u67b6\u63d0\u4f9b\u4e86",Object(i.b)("inlineCode",{parentName:"p"},"[ErrorCodeItemMetadata]")," \u7279\u6027\u7528\u6765\u6807\u8bc6",Object(i.b)("strong",{parentName:"p"},"\u679a\u4e3e\u5b57\u6bb5"),"\u5f02\u5e38\u5143\u6570\u636e\uff0c\u8be5\u7279\u6027\u652f\u6301\u4f20\u5165 ",Object(i.b)("inlineCode",{parentName:"p"},"\u6d88\u606f\u5185\u5bb9")," \u548c ",Object(i.b)("inlineCode",{parentName:"p"},"\u683c\u5f0f\u5316\u53c2\u6570"),"\u3002\u6700\u7ec8\u4f1a\u4f7f\u7528 ",Object(i.b)("inlineCode",{parentName:"p"},"String.Format(\u6d88\u606f\u5185\u5bb9\uff0c\u683c\u5f0f\u5316\u53c2\u6570)")," \u8fdb\u884c\u683c\u5f0f\u5316\u3002"),Object(i.b)("p",null,"\u5982\u679c\u6d88\u606f\u5185\u5bb9\u4e2d\u5305\u542b",Object(i.b)("inlineCode",{parentName:"p"},"\u683c\u5f0f\u5316\u5360\u4f4d\u7b26"),"\u4f46\u672a\u6307\u5b9a",Object(i.b)("inlineCode",{parentName:"p"},"\u683c\u5f0f\u5316\u53c2\u6570"),"\uff0c\u90a3\u4e48\u4f1a\u67e5\u627e\u5f02\u5e38\u6240\u5728\u65b9\u6cd5\u662f\u5426\u8d34\u6709 ",Object(i.b)("inlineCode",{parentName:"p"},"[IfException]")," \u7279\u6027\u4e14\u542b\u6709\u683c\u5f0f\u5316\u53c2\u6570\uff0c\u63a5\u7740\u5c31\u4f1a\u67e5\u627e ",Object(i.b)("inlineCode",{parentName:"p"},"Oops.Oh")," \u4e2d\u6307\u5b9a\u7684 ",Object(i.b)("inlineCode",{parentName:"p"},"\u683c\u5f0f\u5316\u53c2\u6570"),"\u3002"),Object(i.b)("h3",{id:"763-\u9759\u6001\u5f02\u5e38\u7c7b\u4f7f\u7528"},"7.6.3 \u9759\u6001\u5f02\u5e38\u7c7b\u4f7f\u7528"),Object(i.b)("pre",null,Object(i.b)("code",Object(a.a)({parentName:"pre"},{className:"language-cs",metastring:"{2,12}","{2,12}":!0}),"using Fur.DynamicApiController;\nusing Fur.FriendlyException;\n\nnamespace Fur.Application\n{\n    public class FurAppService : IDynamicApiController\n    {\n        public int Get(int id)\n        {\n            if (id < 3)\n            {\n                throw Oops.Oh(ErrorCodes.z1000, id, 3);\n            }\n\n            return id;\n        }\n    }\n}\n")),Object(i.b)("p",null,"\u5982\u4e0b\u56fe\u6240\u793a\uff1a"),Object(i.b)("img",{src:Object(c.a)("img/yhyc3.gif")}),Object(i.b)("h3",{id:"764-\u66f4\u591a\u4f8b\u5b50"},"7.6.4 \u66f4\u591a\u4f8b\u5b50"),Object(i.b)("pre",null,Object(i.b)("code",Object(a.a)({parentName:"pre"},{className:"language-cs"}),'throw Oops.Oh(1000);\nthrow Oops.Oh(ErrorCodes.x1000);\nthrow Oops.Oh("\u54c8\u54c8\u54c8\u54c8");\nthrow Oops.Oh(errorCode: "x1001");\nthrow Oops.Oh(1000, typeof(Exception));\n')),Object(i.b)("h2",{id:"77-\u591a\u4e2a\u5f02\u5e38\u4fe1\u606f\u7c7b\u578b"},"7.7 \u591a\u4e2a\u5f02\u5e38\u4fe1\u606f\u7c7b\u578b"),Object(i.b)("pre",null,Object(i.b)("code",Object(a.a)({parentName:"pre"},{className:"language-cs",metastring:"{5-6,21-22}","{5-6,21-22}":!0}),'using Fur.FriendlyException;\n\nnamespace Fur.Application\n{\n    [ErrorCodeType]\n    public enum ErrorCodes\n    {\n        [ErrorCodeItemMetadata("{0} \u4e0d\u80fd\u5c0f\u4e8e {1}")]\n        z1000,\n\n        [ErrorCodeItemMetadata("\u6570\u636e\u4e0d\u5b58\u5728")]\n        x1000,\n\n        [ErrorCodeItemMetadata("{0} \u53d1\u73b0 {1} \u4e2a\u5f02\u5e38", "\u767e\u5c0f\u50e7", 2)]\n        x1001,\n\n        [ErrorCodeItemMetadata("\u670d\u52a1\u5668\u8fd0\u884c\u5f02\u5e38", ErrorCode = "Error")]\n        SERVER_ERROR\n    }\n\n    [ErrorCodeType]\n    public enum UserErrorCodes\n    {\n        [ErrorCodeItemMetadata("\u7528\u6237\u6570\u636e\u4e0d\u5b58\u5728")]\n        u1000,\n\n        [ErrorCodeItemMetadata("\u5176\u4ed6\u5f02\u5e38")]\n        u1001\n    }\n}\n')),Object(i.b)("div",{className:"admonition admonition-important alert alert--info"},Object(i.b)("div",Object(a.a)({parentName:"div"},{className:"admonition-heading"}),Object(i.b)("h5",{parentName:"div"},Object(i.b)("span",Object(a.a)({parentName:"h5"},{className:"admonition-icon"}),Object(i.b)("svg",Object(a.a)({parentName:"span"},{xmlns:"http://www.w3.org/2000/svg",width:"14",height:"16",viewBox:"0 0 14 16"}),Object(i.b)("path",Object(a.a)({parentName:"svg"},{fillRule:"evenodd",d:"M7 2.3c3.14 0 5.7 2.56 5.7 5.7s-2.56 5.7-5.7 5.7A5.71 5.71 0 0 1 1.3 8c0-3.14 2.56-5.7 5.7-5.7zM7 1C3.14 1 0 4.14 0 8s3.14 7 7 7 7-3.14 7-7-3.14-7-7-7zm1 3H6v5h2V4zm0 6H6v2h2v-2z"})))),"\u7279\u522b\u6ce8\u610f")),Object(i.b)("div",Object(a.a)({parentName:"div"},{className:"admonition-content"}),Object(i.b)("p",{parentName:"div"},"\u591a\u4e2a\u5f02\u5e38\u9759\u6001\u7c7b\u4e2d\u4e5f\u5fc5\u987b\u4fdd\u8bc1\u5e38\u91cf\u503c\u552f\u4e00\u6027\uff0c\u4e0d\u53ef\u91cd\u590d\u3002"))),Object(i.b)("h2",{id:"78-ierrorcodetypeprovider-\u63d0\u4f9b\u5668"},"7.8 ",Object(i.b)("inlineCode",{parentName:"h2"},"IErrorCodeTypeProvider")," \u63d0\u4f9b\u5668"),Object(i.b)("p",null,"\u5728 ",Object(i.b)("inlineCode",{parentName:"p"},"Fur")," \u6846\u67b6\u4e2d\uff0c\u8fd8\u63d0\u4f9b\u4e86 ",Object(i.b)("inlineCode",{parentName:"p"},"IErrorCodeTypeProvider")," \u5f02\u5e38\u6d88\u606f\u63d0\u4f9b\u5668\u63a5\u53e3\uff0c\u65b9\u4fbf\u5728\u4e0d\u80fd\u8d34 ",Object(i.b)("inlineCode",{parentName:"p"},"[ErrorCodeType]")," \u7279\u6027\u60c5\u51b5\u4e0b\u4f7f\u7528\uff1a"),Object(i.b)("pre",null,Object(i.b)("code",Object(a.a)({parentName:"pre"},{className:"language-cs",metastring:"{2,6,8-11}","{2,6,8-11}":!0}),"using Fur.FriendlyException;\nusing System;\n\nnamespace Fur.Application\n{\n    public class CustomErrorCodeTypeProvider : IErrorCodeTypeProvider\n    {\n        public Type[] Definitions => new[] {\n            typeof(ErrorCodes),\n            typeof(ErrorCodes2)\n        };\n    }\n}\n")),Object(i.b)("p",null,"\u542f\u7528 ",Object(i.b)("inlineCode",{parentName:"p"},"IErrorCodeTypeProvider")," \u63d0\u4f9b\u5668\uff1a"),Object(i.b)("pre",null,Object(i.b)("code",Object(a.a)({parentName:"pre"},{className:"language-cs",metastring:'{11} title="Fur.Web.Core\\FurWebCoreStartup.cs"',"{11}":!0,title:'"Fur.Web.Core\\FurWebCoreStartup.cs"'}),"using Microsoft.Extensions.DependencyInjection;\n\nnamespace Fur.Web.Core\n{\n    [AppStartup(800)]\n    public sealed class FurWebCoreStartup : AppStartup\n    {\n        public void ConfigureServices(IServiceCollection services)\n        {\n            services.AddControllers()\n                    .AddFriendlyException<CustomErrorCodeTypeProvider>();\n        }\n    }\n}\n")),Object(i.b)("div",{className:"admonition admonition-tip alert alert--success"},Object(i.b)("div",Object(a.a)({parentName:"div"},{className:"admonition-heading"}),Object(i.b)("h5",{parentName:"div"},Object(i.b)("span",Object(a.a)({parentName:"h5"},{className:"admonition-icon"}),Object(i.b)("svg",Object(a.a)({parentName:"span"},{xmlns:"http://www.w3.org/2000/svg",width:"12",height:"16",viewBox:"0 0 12 16"}),Object(i.b)("path",Object(a.a)({parentName:"svg"},{fillRule:"evenodd",d:"M6.5 0C3.48 0 1 2.19 1 5c0 .92.55 2.25 1 3 1.34 2.25 1.78 2.78 2 4v1h5v-1c.22-1.22.66-1.75 2-4 .45-.75 1-2.08 1-3 0-2.81-2.48-5-5.5-5zm3.64 7.48c-.25.44-.47.8-.67 1.11-.86 1.41-1.25 2.06-1.45 3.23-.02.05-.02.11-.02.17H5c0-.06 0-.13-.02-.17-.2-1.17-.59-1.83-1.45-3.23-.2-.31-.42-.67-.67-1.11C2.44 6.78 2 5.65 2 5c0-2.2 2.02-4 4.5-4 1.22 0 2.36.42 3.22 1.19C10.55 2.94 11 3.94 11 5c0 .66-.44 1.78-.86 2.48zM4 14h5c-.23 1.14-1.3 2-2.5 2s-2.27-.86-2.5-2z"})))),"\u5c0f\u77e5\u8bc6")),Object(i.b)("div",Object(a.a)({parentName:"div"},{className:"admonition-content"}),Object(i.b)("p",{parentName:"div"},"\u53ea\u6709\u4f7f\u7528 ",Object(i.b)("inlineCode",{parentName:"p"},"IErrorCodeTypeProvider")," \u65b9\u5f0f\u624d\u9700\u4f7f\u7528\u6cdb\u578b\u65b9\u5f0f\u6ce8\u518c\u3002\u901a\u8fc7\u4e0a\u9762\u7684\u65b9\u5f0f\u6ce8\u518c\u53ef\u4ee5\u540c\u65f6\u652f\u6301 ",Object(i.b)("inlineCode",{parentName:"p"},"IErrorCodeTypeProvider")," \u548c ",Object(i.b)("inlineCode",{parentName:"p"},"[ErrorCodeType]")," \u65b9\u5f0f\u3002"))),Object(i.b)("h2",{id:"79-appsettingjson-\u4e2d\u914d\u7f6e"},"7.9 ",Object(i.b)("inlineCode",{parentName:"h2"},"appsetting.json")," \u4e2d\u914d\u7f6e"),Object(i.b)("p",null,Object(i.b)("inlineCode",{parentName:"p"},"Fur")," \u6846\u67b6\u8fd8\u63d0\u4f9b\u4e86\u975e\u5e38\u7075\u6d3b\u7684\u914d\u7f6e\u6587\u4ef6\u914d\u7f6e\u5f02\u5e38\uff0c\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\u53ef\u4ee5\u5b9e\u73b0\u5f02\u5e38\u4fe1\u606f\u540e\u671f\u914d\u7f6e\uff0c\u4e5f\u5c31\u662f\u65e0\u9700\u5728\u5f00\u53d1\u9636\u6bb5\u9884\u5148\u5b9a\u4e49\u3002"),Object(i.b)("pre",null,Object(i.b)("code",Object(a.a)({parentName:"pre"},{className:"language-json",metastring:'{2-8} title="Fur.Web.Entry/appsettings.json"',"{2-8}":!0,title:'"Fur.Web.Entry/appsettings.json"'}),'{\n  "ErrorCodeMessageSettings": {\n    "Definitions": [\n      [5000, "{0} \u4e0d\u80fd\u5c0f\u4e8e {1}"],\n      [5001, "\u6211\u53eb {0} \u540d\u5b57", "\u767e\u5c0f\u50e7"],\n      [5002, "Oops! \u51fa\u9519\u4e86"]\n    ]\n  }\n}\n')),Object(i.b)("p",null,Object(i.b)("inlineCode",{parentName:"p"},"Definitions")," \u7c7b\u578b\u4e3a\u4e8c\u7ef4\u6570\u7ec4\uff0c\u4e8c\u7ef4\u6570\u7ec4\u4e2d\u7684\u6bcf\u4e00\u4e2a\u6570\u7ec4\u7b2c\u4e00\u4e2a\u53c2\u6570\u4e3a ",Object(i.b)("inlineCode",{parentName:"p"},"ErrorCode")," \u4e5f\u5c31\u662f\u9519\u8bef\u7801\uff0c\u7b2c\u4e8c\u4e2a\u53c2\u6570\u4e3a ",Object(i.b)("inlineCode",{parentName:"p"},"ErrorMessage")," \u6d88\u606f\u5185\u5bb9\uff0c\u5269\u4f59\u53c2\u6570\u4f5c\u4e3a ",Object(i.b)("inlineCode",{parentName:"p"},"ErrorMessage")," \u7684\u683c\u5f0f\u5316\u53c2\u6570\u3002"),Object(i.b)("h4",{id:"\u4f7f\u7528\u793a\u4f8b"},"\u4f7f\u7528\u793a\u4f8b"),Object(i.b)("pre",null,Object(i.b)("code",Object(a.a)({parentName:"pre"},{className:"language-cs",metastring:"{2,12}","{2,12}":!0}),"using Fur.DynamicApiController;\nusing Fur.FriendlyException;\n\nnamespace Fur.Application\n{\n    public class FurAppService : IDynamicApiController\n    {\n        public int Get(int id)\n        {\n            if (id < 3)\n            {\n                throw Oops.Oh(5000, id, 3); // \u53ef\u4ee5\u5c06 5000\u4f5c\u4e3a\u5e38\u91cf\u914d\u7f6e\u8d77\u6765\n            }\n\n            return id;\n        }\n    }\n}\n")),Object(i.b)("div",{className:"admonition admonition-tip alert alert--success"},Object(i.b)("div",Object(a.a)({parentName:"div"},{className:"admonition-heading"}),Object(i.b)("h5",{parentName:"div"},Object(i.b)("span",Object(a.a)({parentName:"h5"},{className:"admonition-icon"}),Object(i.b)("svg",Object(a.a)({parentName:"span"},{xmlns:"http://www.w3.org/2000/svg",width:"12",height:"16",viewBox:"0 0 12 16"}),Object(i.b)("path",Object(a.a)({parentName:"svg"},{fillRule:"evenodd",d:"M6.5 0C3.48 0 1 2.19 1 5c0 .92.55 2.25 1 3 1.34 2.25 1.78 2.78 2 4v1h5v-1c.22-1.22.66-1.75 2-4 .45-.75 1-2.08 1-3 0-2.81-2.48-5-5.5-5zm3.64 7.48c-.25.44-.47.8-.67 1.11-.86 1.41-1.25 2.06-1.45 3.23-.02.05-.02.11-.02.17H5c0-.06 0-.13-.02-.17-.2-1.17-.59-1.83-1.45-3.23-.2-.31-.42-.67-.67-1.11C2.44 6.78 2 5.65 2 5c0-2.2 2.02-4 4.5-4 1.22 0 2.36.42 3.22 1.19C10.55 2.94 11 3.94 11 5c0 .66-.44 1.78-.86 2.48zM4 14h5c-.23 1.14-1.3 2-2.5 2s-2.27-.86-2.5-2z"})))),"\u5c0f\u77e5\u8bc6")),Object(i.b)("div",Object(a.a)({parentName:"div"},{className:"admonition-content"}),Object(i.b)("p",{parentName:"div"},Object(i.b)("inlineCode",{parentName:"p"},"[ErrorCodeType]")," \u548c ",Object(i.b)("inlineCode",{parentName:"p"},"IErrorCodeTypeProvider")," \u548c ",Object(i.b)("inlineCode",{parentName:"p"},"appsettings.json")," \u53ef\u4ee5\u540c\u65f6\u4f7f\u7528\u3002"))),Object(i.b)("h2",{id:"710-ifexception-\u4f7f\u7528"},"7.10 ",Object(i.b)("inlineCode",{parentName:"h2"},"[IfException]")," \u4f7f\u7528"),Object(i.b)("p",null,Object(i.b)("inlineCode",{parentName:"p"},"Fur")," \u6846\u67b6\u63d0\u4f9b\u4e86 ",Object(i.b)("inlineCode",{parentName:"p"},"[IfException]")," \u7279\u6027\u53ef\u4ee5",Object(i.b)("strong",{parentName:"p"},"\u8986\u76d6\u9ed8\u8ba4\u6d88\u606f\u914d\u7f6e"),"\u3002\u4e5f\u5c31\u662f\u8986\u76d6 ",Object(i.b)("inlineCode",{parentName:"p"},"\u5f02\u5e38\u6d88\u606f\u7c7b\u578b")," \u548c ",Object(i.b)("inlineCode",{parentName:"p"},"appsettings.json")," \u4e2d\u7684\u914d\u7f6e\u3002"),Object(i.b)("div",{className:"admonition admonition-caution alert alert--warning"},Object(i.b)("div",Object(a.a)({parentName:"div"},{className:"admonition-heading"}),Object(i.b)("h5",{parentName:"div"},Object(i.b)("span",Object(a.a)({parentName:"h5"},{className:"admonition-icon"}),Object(i.b)("svg",Object(a.a)({parentName:"span"},{xmlns:"http://www.w3.org/2000/svg",width:"16",height:"16",viewBox:"0 0 16 16"}),Object(i.b)("path",Object(a.a)({parentName:"svg"},{fillRule:"evenodd",d:"M8.893 1.5c-.183-.31-.52-.5-.887-.5s-.703.19-.886.5L.138 13.499a.98.98 0 0 0 0 1.001c.193.31.53.501.886.501h13.964c.367 0 .704-.19.877-.5a1.03 1.03 0 0 0 .01-1.002L8.893 1.5zm.133 11.497H6.987v-2.003h2.039v2.003zm0-3.004H6.987V5.987h2.039v4.006z"})))),"\u7279\u522b\u6ce8\u610f")),Object(i.b)("div",Object(a.a)({parentName:"div"},{className:"admonition-content"}),Object(i.b)("p",{parentName:"div"},Object(i.b)("inlineCode",{parentName:"p"},"[IfException]")," \u53ea\u80fd\u8d34\u5728\u65b9\u6cd5\u4e0a\uff0c\u652f\u6301\u591a\u4e2a\uff0c\u800c\u4e14\u8be5\u65b9\u6cd5\u6240\u5728\u7684\u7c7b\u7c7b\u578b\u5fc5\u987b\u662f ",Object(i.b)("inlineCode",{parentName:"p"},"ControllerBase")," \u5b50\u7c7b \u6216 \u5b9e\u73b0 ",Object(i.b)("inlineCode",{parentName:"p"},"IDynamicApiController")," \u63a5\u53e3\u3002"))),Object(i.b)("h3",{id:"7101-\u4f7f\u7528\u793a\u4f8b"},"7.10.1 \u4f7f\u7528\u793a\u4f8b"),Object(i.b)("ul",null,Object(i.b)("li",{parentName:"ul"},"\u5f02\u5e38\u6d88\u606f\u7c7b\u5b9a\u4e49")),Object(i.b)("pre",null,Object(i.b)("code",Object(a.a)({parentName:"pre"},{className:"language-cs",metastring:"{1,4}","{1,4}":!0}),'[ErrorCodeType]\npublic static class ErrorCodes\n{\n   [ErrorCodeItemMetadata("{0} \u4e0d\u80fd\u5c0f\u4e8e {1}")]\n   z1000\n}\n')),Object(i.b)("ul",null,Object(i.b)("li",{parentName:"ul"},"\u8986\u76d6\u9ed8\u8ba4\u914d\u7f6e")),Object(i.b)("pre",null,Object(i.b)("code",Object(a.a)({parentName:"pre"},{className:"language-cs",metastring:"{8}","{8}":!0}),'using Fur.DynamicApiController;\nusing Fur.FriendlyException;\n\nnamespace Fur.Application\n{\n    public class FurAppService : IDynamicApiController\n    {\n        [IfException(ErrorCodes.z1000, ErrorMessage = "\u6211\u8986\u76d6\u4e86\u9ed8\u8ba4\u7684\uff1a{0} \u4e0d\u80fd\u5c0f\u4e8e {1}")]\n        public int Get(int id)\n        {\n            if (id < 3)\n            {\n                throw Oops.Oh(ErrorCodes.z1000, id, 3);\n            }\n\n            return id;\n        }\n    }\n}\n')),Object(i.b)("p",null,"\u5982\u4e0b\u56fe\u6240\u793a\uff1a"),Object(i.b)("img",{src:Object(c.a)("img/yhyc4.png")}),Object(i.b)("h3",{id:"7102-\u66f4\u591a\u4f8b\u5b50"},"7.10.2 \u66f4\u591a\u4f8b\u5b50"),Object(i.b)("pre",null,Object(i.b)("code",Object(a.a)({parentName:"pre"},{className:"language-cs",metastring:"{2,8-11}","{2,8-11}":!0}),'using Fur.DynamicApiController;\nusing Fur.FriendlyException;\n\nnamespace Fur.Application\n{\n    public class FurAppService : IDynamicApiController\n    {\n        [IfException(ErrorCodes.z1000, ErrorMessage = "\u6211\u8986\u76d6\u4e86\u9ed8\u8ba4\u7684\uff1a{0} \u4e0d\u80fd\u5c0f\u4e8e {1}")]\n        [IfException(ErrorCodes.x1001, "\u683c\u5f0f\u5316\u53c2\u65701", "\u683c\u5f0f\u5316\u53c2\u65702", ErrorMessage = "\u6211\u8986\u76d6\u4e86\u9ed8\u8ba4\u7684\uff1a{0} \u4e0d\u80fd\u5c0f\u4e8e {1}")]\n        [IfException(ErrorCodes.x1000, "\u683c\u5f0f\u5316\u53c2\u65701", "\u683c\u5f0f\u5316\u53c2\u65702")]\n        [IfException(ErrorCodes.SERVER_ERROR, "\u683c\u5f0f\u5316\u53c2\u65701", "\u683c\u5f0f\u5316\u53c2\u65702")]\n        public int Get(int id)\n        {\n            if (id < 3)\n            {\n                throw Oops.Oh(ErrorCodes.z1000, id, 3);\n            }\n\n            return id;\n        }\n    }\n}\n')),Object(i.b)("div",{className:"admonition admonition-important alert alert--info"},Object(i.b)("div",Object(a.a)({parentName:"div"},{className:"admonition-heading"}),Object(i.b)("h5",{parentName:"div"},Object(i.b)("span",Object(a.a)({parentName:"h5"},{className:"admonition-icon"}),Object(i.b)("svg",Object(a.a)({parentName:"span"},{xmlns:"http://www.w3.org/2000/svg",width:"14",height:"16",viewBox:"0 0 14 16"}),Object(i.b)("path",Object(a.a)({parentName:"svg"},{fillRule:"evenodd",d:"M7 2.3c3.14 0 5.7 2.56 5.7 5.7s-2.56 5.7-5.7 5.7A5.71 5.71 0 0 1 1.3 8c0-3.14 2.56-5.7 5.7-5.7zM7 1C3.14 1 0 4.14 0 8s3.14 7 7 7 7-3.14 7-7-3.14-7-7-7zm1 3H6v5h2V4zm0 6H6v2h2v-2z"})))),"\u683c\u5f0f\u5316\u6d41\u7a0b")),Object(i.b)("div",Object(a.a)({parentName:"div"},{className:"admonition-content"}),Object(i.b)("p",{parentName:"div"},"\u5982\u679c\u6d88\u606f\u5185\u5bb9\u4e2d\u5305\u542b",Object(i.b)("inlineCode",{parentName:"p"},"\u683c\u5f0f\u5316\u5360\u4f4d\u7b26"),"\u4f46\u672a\u6307\u5b9a",Object(i.b)("inlineCode",{parentName:"p"},"\u683c\u5f0f\u5316\u53c2\u6570"),"\uff0c\u90a3\u4e48\u4f1a\u67e5\u627e\u5f02\u5e38\u6240\u5728\u65b9\u6cd5\u662f\u5426\u8d34\u6709 ",Object(i.b)("inlineCode",{parentName:"p"},"[IfException]")," \u7279\u6027\u4e14\u542b\u6709\u683c\u5f0f\u5316\u53c2\u6570\uff0c\u63a5\u7740\u5c31\u4f1a\u67e5\u627e ",Object(i.b)("inlineCode",{parentName:"p"},"Oops.Oh")," \u4e2d\u6307\u5b9a\u7684 ",Object(i.b)("inlineCode",{parentName:"p"},"\u683c\u5f0f\u5316\u53c2\u6570"),"\u3002"))),Object(i.b)("h2",{id:"711-\u5f02\u5e38\u6d88\u606f\u4f18\u5148\u7ea7"},"7.11 \u5f02\u5e38\u6d88\u606f\u4f18\u5148\u7ea7"),Object(i.b)("p",null,Object(i.b)("inlineCode",{parentName:"p"},"[ErrorCodeItemMetadata]")," -> ",Object(i.b)("inlineCode",{parentName:"p"},"appsettings.json")," -> ",Object(i.b)("inlineCode",{parentName:"p"},"[IfException]"),"\u3002",Object(i.b)("strong",{parentName:"p"},"\uff08\u4f4e -> \u9ad8\uff09")),Object(i.b)("ul",null,Object(i.b)("li",{parentName:"ul"},Object(i.b)("inlineCode",{parentName:"li"},"[IfException]")," \u4f1a\u8986\u76d6 ",Object(i.b)("inlineCode",{parentName:"li"},"appsettings.json")," \u5b9a\u4e49\u7684\u72b6\u6001\u7801\u6d88\u606f\u3002"),Object(i.b)("li",{parentName:"ul"},Object(i.b)("inlineCode",{parentName:"li"},"appsettings.json")," \u4f1a\u8986\u76d6 ",Object(i.b)("inlineCode",{parentName:"li"},"[ErrorCodeItemMetadata]")," \u5b9a\u4e49\u7684\u6d88\u606f\u3002")),Object(i.b)("h2",{id:"712-\u591a\u8bed\u8a00\u652f\u6301"},"7.12 \u591a\u8bed\u8a00\u652f\u6301"),Object(i.b)("p",null,"\u6587\u6863\u6574\u7406\u4e2d..."),Object(i.b)("h2",{id:"713-\u5f02\u5e38\u6a21\u578b\u63d0\u4f9b\u5668"},"7.13 \u5f02\u5e38\u6a21\u578b\u63d0\u4f9b\u5668"),Object(i.b)("p",null,Object(i.b)("inlineCode",{parentName:"p"},"Fur")," \u6846\u67b6\u63d0\u4f9b ",Object(i.b)("inlineCode",{parentName:"p"},"\u89c4\u8303\u5316\u7ed3\u679c")," \u529f\u80fd\uff0c\u53ef\u4ee5\u901a\u8fc7\u5b9e\u73b0 ",Object(i.b)("inlineCode",{parentName:"p"},"IUnifyResultProvider")," \u63d0\u4f9b\u5668\u5b9e\u73b0\u5f02\u5e38\u8fd4\u56de\u503c\u5b9a\u5236\uff0c\u5982\uff1a",Object(i.b)("inlineCode",{parentName:"p"},"RESTfulResultProvider"),"\uff1a"),Object(i.b)("pre",null,Object(i.b)("code",Object(a.a)({parentName:"pre"},{className:"language-cs"}),'using Microsoft.AspNetCore.Http;\nusing Microsoft.AspNetCore.Mvc;\nusing Microsoft.AspNetCore.Mvc.Filters;\nusing Microsoft.AspNetCore.Mvc.ModelBinding;\nusing System.Collections.Generic;\n\nnamespace Fur.UnifyResult\n{\n    /// <summary>\n    /// RESTful \u98ce\u683c\u8fd4\u56de\u503c\n    /// </summary>\n    public class RESTfulResultProvider : IUnifyResultProvider\n    {\n        /// <summary>\n        /// \u5f02\u5e38\u8fd4\u56de\u503c\n        /// </summary>\n        /// <param name="context"></param>\n        /// <returns></returns>\n        public IActionResult OnException(ExceptionContext context)\n        {\n            return new JsonResult(new RESTfulResult\n            {\n                StatusCode = StatusCodes.Status500InternalServerError,\n                Successed = false,\n                Data = null,\n                Errors = context.Exception.Message\n            });\n        }\n\n        /// <summary>\n        /// \u6210\u529f\u8fd4\u56de\u503c\n        /// </summary>\n        /// <param name="context"></param>\n        /// <returns></returns>\n        public IActionResult OnSuccessed(ActionExecutedContext context)\n        {\n            object data;\n            // \u5904\u7406\u5185\u5bb9\u7ed3\u679c\n            if (context.Result is ContentResult contentResult) data = contentResult.Content;\n            // \u5904\u7406\u5bf9\u8c61\u7ed3\u679c\n            else if (context.Result is ObjectResult objectResult) data = objectResult.Value;\n            else return null;\n\n            return new JsonResult(new RESTfulResult\n            {\n                StatusCode = context.Result is EmptyResult ? StatusCodes.Status204NoContent : StatusCodes.Status200OK,\n                Successed = true,\n                Data = data,\n                Errors = null\n            });\n        }\n\n        /// <summary>\n        /// \u9a8c\u8bc1\u5931\u8d25\u8fd4\u56de\u503c\n        /// </summary>\n        /// <param name="context"></param>\n        /// <param name="modelStates"></param>\n        /// <param name="validationResults"></param>\n        /// <param name="validateFaildMessage"></param>\n        /// <returns></returns>\n        public IActionResult OnValidateFailed(ActionExecutingContext context, ModelStateDictionary modelStates, Dictionary<string, IEnumerable<string>> validationResults, string validateFaildMessage)\n        {\n            return new JsonResult(new RESTfulResult\n            {\n                StatusCode = StatusCodes.Status400BadRequest,\n                Successed = false,\n                Data = null,\n                Errors = validationResults\n            });\n        }\n    }\n}\n')),Object(i.b)("p",null,"\u4e4b\u540e\u5728 ",Object(i.b)("inlineCode",{parentName:"p"},"Startup.cs")," \u4e2d\u6ce8\u518c\u5373\u53ef\uff1a"),Object(i.b)("pre",null,Object(i.b)("code",Object(a.a)({parentName:"pre"},{className:"language-cs"}),"services.AddControllers().AddUnifyResult<RESTfulResult, RESTfulResultProvider>();\n")),Object(i.b)("h2",{id:"714-\u5168\u5c40\u5f02\u5e38\u5904\u7406\u63d0\u4f9b\u5668"},"7.14 \u5168\u5c40\u5f02\u5e38\u5904\u7406\u63d0\u4f9b\u5668"),Object(i.b)("p",null,"\u901a\u5e38\u6211\u4eec\u9700\u8981\u5728\u5f02\u5e38\u6355\u83b7\u7684\u65f6\u5019\u5199\u65e5\u5fd7\uff0c\u8fd9\u65f6\u5019\u5c31\u9700\u8981\u4f7f\u7528\u5230 ",Object(i.b)("inlineCode",{parentName:"p"},"IGlobalExceptionHandler")," \u5f02\u5e38\u5b9a\u4e49\u5904\u7406\u7a0b\u5e8f\uff0c\u5982\uff1a"),Object(i.b)("pre",null,Object(i.b)("code",Object(a.a)({parentName:"pre"},{className:"language-cs",metastring:"{8}","{8}":!0}),"using Fur.DependencyInjection;\nusing Fur.FriendlyException;\nusing Microsoft.AspNetCore.Mvc.Filters;\nusing System.Threading.Tasks;\n\nnamespace Fur.Application\n{\n    public class LogExceptionHandler : IGlobalExceptionHandler, ISingleton\n    {\n        public Task OnExceptionAsync(ExceptionContext context)\n        {\n            // \u5199\u65e5\u5fd7\n\n            return Task.CompletedTask;\n        }\n    }\n}\n")),Object(i.b)("h2",{id:"715-\u53cd\u9988\u4e0e\u5efa\u8bae"},"7.15 \u53cd\u9988\u4e0e\u5efa\u8bae"),Object(i.b)("div",{className:"admonition admonition-note alert alert--secondary"},Object(i.b)("div",Object(a.a)({parentName:"div"},{className:"admonition-heading"}),Object(i.b)("h5",{parentName:"div"},Object(i.b)("span",Object(a.a)({parentName:"h5"},{className:"admonition-icon"}),Object(i.b)("svg",Object(a.a)({parentName:"span"},{xmlns:"http://www.w3.org/2000/svg",width:"14",height:"16",viewBox:"0 0 14 16"}),Object(i.b)("path",Object(a.a)({parentName:"svg"},{fillRule:"evenodd",d:"M6.3 5.69a.942.942 0 0 1-.28-.7c0-.28.09-.52.28-.7.19-.18.42-.28.7-.28.28 0 .52.09.7.28.18.19.28.42.28.7 0 .28-.09.52-.28.7a1 1 0 0 1-.7.3c-.28 0-.52-.11-.7-.3zM8 7.99c-.02-.25-.11-.48-.31-.69-.2-.19-.42-.3-.69-.31H6c-.27.02-.48.13-.69.31-.2.2-.3.44-.31.69h1v3c.02.27.11.5.31.69.2.2.42.31.69.31h1c.27 0 .48-.11.69-.31.2-.19.3-.42.31-.69H8V7.98v.01zM7 2.3c-3.14 0-5.7 2.54-5.7 5.68 0 3.14 2.56 5.7 5.7 5.7s5.7-2.55 5.7-5.7c0-3.15-2.56-5.69-5.7-5.69v.01zM7 .98c3.86 0 7 3.14 7 7s-3.14 7-7 7-7-3.12-7-7 3.14-7 7-7z"})))),"\u4e0e\u6211\u4eec\u4ea4\u6d41")),Object(i.b)("div",Object(a.a)({parentName:"div"},{className:"admonition-content"}),Object(i.b)("p",{parentName:"div"},"\u7ed9 Fur \u63d0 ",Object(i.b)("a",Object(a.a)({parentName:"p"},{href:"https://gitee.com/monksoul/Fur/issues/new?issue"}),"Issue"),"\u3002"))))}s.isMDXComponent=!0}}]);