1"use strict";(self.webpackChunkdoc=self.webpackChunkdoc||[]).push([[1571],{3905:(e,r,n)=>{n.d(r,{Zo:()=>p,kt:()=>m});var t=n(7294);function o(e,r,n){return r in e?Object.defineProperty(e,r,{value:n,enumerable:!0,configurable:!0,writable:!0}):e[r]=n,e}function a(e,r){var n=Object.keys(e);if(Object.getOwnPropertySymbols){var t=Object.getOwnPropertySymbols(e);r&&(t=t.filter((function(r){return Object.getOwnPropertyDescriptor(e,r).enumerable}))),n.push.apply(n,t)}return n}function i(e){for(var r=1;r<arguments.length;r++){var n=null!=arguments[r]?arguments[r]:{};r%2?a(Object(n),!0).forEach((function(r){o(e,r,n[r])})):Object.getOwnPropertyDescriptors?Object.defineProperties(e,Object.getOwnPropertyDescriptors(n)):a(Object(n)).forEach((function(r){Object.defineProperty(e,r,Object.getOwnPropertyDescriptor(n,r))}))}return e}function l(e,r){if(null==e)return{};var n,t,o=function(e,r){if(null==e)return{};var n,t,o={},a=Object.keys(e);for(t=0;t<a.length;t++)n=a[t],r.indexOf(n)>=0||(o[n]=e[n]);return o}(e,r);if(Object.getOwnPropertySymbols){var a=Object.getOwnPropertySymbols(e);for(t=0;t<a.length;t++)n=a[t],r.indexOf(n)>=0||Object.prototype.propertyIsEnumerable.call(e,n)&&(o[n]=e[n])}return o}var c=t.createContext({}),s=function(e){var r=t.useContext(c),n=r;return e&&(n="function"==typeof e?e(r):i(i({},r),e)),n},p=function(e){var r=s(e.components);return t.createElement(c.Provider,{value:r},e.children)},u={inlineCode:"code",wrapper:function(e){var r=e.children;return t.createElement(t.Fragment,{},r)}},d=t.forwardRef((function(e,r){var n=e.components,o=e.mdxType,a=e.originalType,c=e.parentName,p=l(e,["components","mdxType","originalType","parentName"]),d=s(n),m=o,f=d["".concat(c,".").concat(m)]||d[m]||u[m]||a;return n?t.createElement(f,i(i({ref:r},p),{},{components:n})):t.createElement(f,i({ref:r},p))}));function m(e,r){var n=arguments,o=r&&r.mdxType;if("string"==typeof e||o){var a=n.length,i=new Array(a);i[0]=d;var l={};for(var c in r)hasOwnProperty.call(r,c)&&(l[c]=r[c]);l.originalType=e,l.mdxType="string"==typeof e?e:o,i[1]=l;for(var s=2;s<a;s++)i[s]=n[s];return t.createElement.apply(null,i)}return t.createElement.apply(null,n)}d.displayName="MDXCreateElement"},1272:(e,r,n)=>{n.r(r),n.d(r,{assets:()=>c,contentTitle:()=>i,default:()=>u,frontMatter:()=>a,metadata:()=>l,toc:()=>s});var t=n(7462),o=(n(7294),n(3905));const a={},i="Handling Errors",l={unversionedId:"develop/error",id:"develop/error",title:"Handling Errors",description:'VulcanSQL SQL Templates, like most programming languages, support error handling. To define an exception in your SQL templates, use the {% error "ERROR_CODE" %} syntax. When the template encounters an error during execution, VulcanSQL halts further execution and sends an error code to the client, rather than returning query results.',source:"@site/docs/develop/error.mdx",sourceDirName:"develop",slug:"/develop/error",permalink:"/docs/develop/error",draft:!1,editUrl:"https://github.com/Canner/vulcan-sql/tree/main/packages/doc/docs/develop/error.mdx",tags:[],version:"current",frontMatter:{},sidebar:"tutorialSidebar",previous:{title:"Caching Datasets",permalink:"/docs/develop/cache"},next:{title:"Validating API Parameters",permalink:"/docs/develop/validator"}},c={},s=[],p={toc:s};
1function u(e){let{components:r,...n}=e;return(0,o.kt)("wrapper",(0,t.Z)({},p,n,{components:r,mdxType:"MDXLayout"}),(0,o.kt)("h1",{id:"handling-errors"},"Handling Errors"),(0,o.kt)("p",null,"VulcanSQL SQL Templates, like most programming languages, support error handling. To define an exception in your SQL templates, use the ",(0,o.kt)("inlineCode",{parentName:"p"},'{% error "ERROR_CODE" %}')," syntax. When the template encounters an error during execution, VulcanSQL halts further execution and sends an error code to the client, rather than returning query results."),(0,o.kt)("p",null,"Consider the following example, where a check is performed before executing the main query:"),(0,o.kt)("pre",null,(0,o.kt)("code",{parentName:"pre",className:"language-sql"},'{% req user %}\nSELECT COUNT(*) AS count FROM customers WHERE name = {{ context.params.name }}\n{% endreq %}\n\n{% if user.value()[0].count == 0 %}\n {% error "CUSTOMER_NOT_FOUND" %}\n{% endif %}\n\nSELECT * FROM customers\nWHERE name = {{ context.params.name }}\nLIMIT 1\n')),(0,o.kt)("p",null,"In this case, if clients send API requests with an invalid name, they will receive a ",(0,o.kt)("inlineCode",{parentName:"p"},"400")," error along with the error code ",(0,o.kt)("inlineCode",{parentName:"p"},"CUSTOMER_NOT_FOUND"),", instead of an empty array. This approach allows you to gracefully handle errors and provide meaningful feedback to the client."))}u.isMDXComponent=!0}}]);
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.