{"id":13713,"date":"2022-08-25T13:18:18","date_gmt":"2022-08-25T20:18:18","guid":{"rendered":"https:\/\/blog.couchbase.com\/?p=13687"},"modified":"2025-06-13T17:32:00","modified_gmt":"2025-06-14T00:32:00","slug":"from-n1ql-to-javascript-and-back-part-4","status":"publish","type":"post","link":"https:\/\/www.couchbase.com\/blog\/from-n1ql-to-javascript-and-back-part-4\/","title":{"rendered":"From N1QL to JavaScript and back &#8211; Part 4: Error Handling"},"content":{"rendered":"<p><span style=\"font-weight: 400\">In previous blogs, we have covered executing N1QL from JavaScript <a href=\"https:\/\/www.couchbase.com\/blog\/from-n1ql-to-javascript-and-back-part-1-introduction\/\" target=\"_blank\" rel=\"noopener\">functions<\/a>, processing documents <a href=\"https:\/\/www.couchbase.com\/blog\/from-n1ql-to-javascript-and-back-part-3\/\" target=\"_blank\" rel=\"noopener\">through iterators<\/a>, and <a href=\"https:\/\/www.couchbase.com\/blog\/from-n1ql-to-javascript-and-back-part-2\/\" target=\"_blank\" rel=\"noopener\">manipulating data<\/a>.<\/span><\/p>\n<p><span style=\"font-weight: 400\">We now move onto handling errors from N1QL statements.<\/span><\/p>\n<h2><span style=\"font-weight: 400\">Error handling<\/span><\/h2>\n<p><span style=\"font-weight: 400\">When an error of any nature is encountered, the <em>jsevaluator<\/em> by default will halt the function execution and return an error:<\/span><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-13689\" src=\"https:\/\/www.couchbase.com\/blog\/wp-content\/uploads\/2022\/08\/image_2022-08-25_140128441.png\" alt=\"\" width=\"911\" height=\"377\" \/><\/p>\n<p><span style=\"font-weight: 400\">In this particular case, the second INSERT will fail because key <\/span><em><span style=\"font-weight: 400\">k10<\/span><\/em><span style=\"font-weight: 400\"> already exists:<\/span><\/p>\n<pre class=\"nums:false lang:js decode:true\">[\r\n\u00a0\u00a0{\r\n\u00a0\u00a0\u00a0\u00a0\"code\": 10109,\r\n\u00a0\u00a0\u00a0\u00a0\"msg\": \"Error executing function 'doInsert' (n1ql:doInsert)\",\r\n\u00a0\u00a0\u00a0\u00a0\"reason\": {\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\"details\": {\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\"Code\": \"\u00a0 \u00a0 var q2 = N1QL('insert into b1 values(\\\\\\\"k10\\\\\\\", {\\\\\\\"f1\\\\\\\": 10});', {}, true);\",\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\"Exception\": {\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\"caller\": \"couchbase:2088\",\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\"cause\": {\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\"caller\": \"couchbase:1961\",\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\"code\": 17012,\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\"key\": \"dml.statement.duplicatekey\",\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\"message\": \"Duplicate Key: k10\"\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0},\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\"code\": 12009,\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\"icause\": \"Duplicate Key: k10\",\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\"key\": \"datastore.couchbase.DML_error\",\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\"message\": \"DML Error, possible causes include concurrent modification. Failed to perform INSERT on key k10\",\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\"retry\": false\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0},\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\"Location\": \"functions\/n1ql.js:21\",\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\"Stack\": \" \u00a0 at doInsert (functions\/n1ql.js:3:14)\"\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0},\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\"type\": \"Exceptions from JS code\"\r\n\u00a0\u00a0\u00a0\u00a0}\r\n\u00a0\u00a0}\r\n]<\/pre>\n<p><span style=\"font-weight: 400\">You don&#8217;t have to stick to the default behaviour, JavaScript allows to catch errors and handle them:<\/span><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-13690\" src=\"https:\/\/www.couchbase.com\/blog\/wp-content\/uploads\/2022\/08\/image_2022-08-25_140409911.png\" alt=\"\" width=\"901\" height=\"243\" \/><\/p>\n<p><span style=\"font-weight: 400\">In this case, the function <\/span><em><span style=\"font-weight: 400\">doInsert()<\/span><\/em><span style=\"font-weight: 400\"> returns <\/span><span style=\"font-weight: 400\">failure<\/span><span style=\"font-weight: 400\"> because key <\/span><em><span style=\"font-weight: 400\">k10<\/span><\/em><span style=\"font-weight: 400\"> already exists.<\/span><\/p>\n<h3><span style=\"font-weight: 400\">Return vs throw<\/span><\/h3>\n<p><span style=\"font-weight: 400\">The <\/span><em><span style=\"font-weight: 400\">catch<\/span><\/em><span style=\"font-weight: 400\"> block can be used to handle the failure in whichever way is useful for your business logic.<\/span><\/p>\n<p><span style=\"font-weight: 400\">The available options boil down to 1) take some evasive action and continue execution, 2) return early with success, and 3) return early with failure.<\/span><\/p>\n<p><span style=\"font-weight: 400\">The difference between success and failure is simply using <\/span><span style=\"font-weight: 400\">return<\/span><span style=\"font-weight: 400\"> to return a value as in the example above vs <\/span><em><span style=\"font-weight: 400\">throw<\/span><\/em><span style=\"font-weight: 400\"> to return an error.<\/span><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-13691\" src=\"https:\/\/www.couchbase.com\/blog\/wp-content\/uploads\/2022\/08\/image_2022-08-25_140640705.png\" alt=\"\" width=\"961\" height=\"265\" \/><\/p>\n<p><span style=\"font-weight: 400\">The difference between the two is important, because <\/span><b>if you choose to return a result, the caller request will continue execution. <\/b><span style=\"font-weight: 400\">While if you throw an error, the whole request will fail with the error, so do be mindful about which you want to use, because it will make the difference between your request working correctly and misbehaving.<\/span><\/p>\n<h3><span style=\"font-weight: 400\">Returned expression<\/span><\/h3>\n<p><span style=\"font-weight: 400\">The second thing to bear in mind is that, whether you return or throw an error, you may very well want to give meaningful information.<\/span><\/p>\n<p><span style=\"font-weight: 400\">Consider the following code:<\/span><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-13692\" src=\"https:\/\/www.couchbase.com\/blog\/wp-content\/uploads\/2022\/08\/image_2022-08-25_140813511.png\" alt=\"\" width=\"943\" height=\"252\" \/><\/p>\n<p><span style=\"font-weight: 400\">This will fail with something like:<\/span><\/p>\n<pre class=\"nums:false lang:js decode:true\">[\r\n\u00a0\u00a0{\r\n\u00a0\u00a0\u00a0\u00a0\"code\": 10109,\r\n\u00a0\u00a0\u00a0\u00a0\"msg\": \"Error executing function 'doInsert' (n1ql:doInsert)\",\r\n\u00a0\u00a0\u00a0\u00a0\"reason\": {\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\"details\": {\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\"Code\": \" \u00a0 \u00a0 \u00a0 throw\u00a0 err;\",\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\"Exception\": {\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\"caller\": \"couchbase:2088\",\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\"cause\": {\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\"caller\": \"couchbase:1961\",\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\"code\": 17012,\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\"key\": \"dml.statement.duplicatekey\",\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\"message\": \"Duplicate Key: k10\"\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0},\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\"code\": 12009,\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\"icause\": \"Duplicate Key: k10\",\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\"key\": \"datastore.couchbase.DML_error\",\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\"message\": \"DML Error, possible causes include concurrent modification. Failed to perform INSERT on key k10\",\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\"retry\": false\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0},\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\"Location\": \"functions\/n1ql.js:6\",\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\"Stack\": \" \u00a0 at doInsert (functions\/n1ql.js:3:17)\"\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0},\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\"type\": \"Exceptions from JS code\"\r\n\u00a0\u00a0\u00a0\u00a0}\r\n\u00a0\u00a0}\r\n]<\/pre>\n<p><span style=\"font-weight: 400\">Much like the previous example, what we gather from this is that\u00a0<\/span><em><span style=\"font-weight: 400\">err<\/span><\/em><span style=\"font-weight: 400\"> is an object, and is returned in the <\/span><em><span style=\"font-weight: 400\">reason<\/span><\/em><span style=\"font-weight: 400\"> field.<\/span><\/p>\n<p><span style=\"font-weight: 400\">However, if you amend the <\/span><em><span style=\"font-weight: 400\">throw<\/span><\/em><span style=\"font-weight: 400\"> statement like so:<\/span><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-13693\" src=\"https:\/\/www.couchbase.com\/blog\/wp-content\/uploads\/2022\/08\/image_2022-08-25_141122014.png\" alt=\"\" width=\"864\" height=\"238\" \/><\/p>\n<p><span style=\"font-weight: 400\">The <\/span><em><span style=\"font-weight: 400\">doInsert()<\/span><\/em><span style=\"font-weight: 400\"> function will now throw a string and not an object:<\/span><\/p>\n<pre class=\"nums:false lang:js decode:true\">[\r\n\u00a0\u00a0{\r\n\u00a0\u00a0\u00a0\u00a0\"code\": 10109,\r\n\u00a0\u00a0\u00a0\u00a0\"msg\": \"Error executing function 'doInsert' (n1ql:doInsert)\",\r\n\u00a0\u00a0\u00a0\u00a0\"reason\": {\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\"details\": {\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\"Code\": \" \u00a0 \u00a0 \u00a0 throw \\\"failure\\\" + err;\",\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\"Exception\": \"failure {\\\"caller\\\":\\\"couchbase:2088\\\",\\\"cause\\\":{\\\"caller\\\":\\\"couchbase:1961\\\",\\\"code\\\":17012,\\\"key\\\":\\\"dml.statement.duplicatekey\\\",\\\"message\\\":\\\"Duplicate Key: k10\\\"},\\\"code\\\":12009,\\\"icause\\\":\\\"Duplicate Key: k10\\\",\\\"key\\\":\\\"datastore.couchbase.DML_error\\\",\\\"message\\\":\\\"DML Error, possible causes include concurrent modification. Failed to perform INSERT on key k10\\\",\\\"retry\\\":false}\",\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\"Location\": \"functions\/n1ql.js:6\"\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0},\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\"type\": \"Exceptions from JS code\"\r\n\u00a0\u00a0\u00a0\u00a0}\r\n\u00a0\u00a0}\r\n]<\/pre>\n<p><span style=\"font-weight: 400\">The <\/span><em><span style=\"font-weight: 400\">err<\/span><\/em><span style=\"font-weight: 400\"> object is now marshalled into a string, and all of a sudden, your error is not as legible.<\/span><\/p>\n<p><span style=\"font-weight: 400\">This applies to <em>return<\/em> too: in particular, by default <\/span><em><span style=\"font-weight: 400\">err<\/span><\/em><span style=\"font-weight: 400\"> contains strings containing marshalled JSON objects.<\/span><\/p>\n<p><span style=\"font-weight: 400\">For instance:<\/span><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-13694\" src=\"https:\/\/www.couchbase.com\/blog\/wp-content\/uploads\/2022\/08\/image_2022-08-25_141322293.png\" alt=\"\" width=\"934\" height=\"242\" \/><\/p>\n<p><span style=\"font-weight: 400\">Returns:<\/span><\/p>\n<pre class=\"nums:false lang:js decode:true\">[\r\n\u00a0\u00a0{\r\n\u00a0\u00a0\u00a0\u00a0\"message\": \"{\\\"caller\\\":\\\"couchbase:2088\\\",\\\"cause\\\":{\\\"caller\\\":\\\"couchbase:1961\\\",\\\"code\\\":17012,\\\"key\\\":\\\"dml.statement.duplicatekey\\\",\\\"message\\\":\\\"Duplicate Key: k10\\\"},\\\"code\\\":12009,\\\"icause\\\":\\\"Duplicate Key: k10\\\",\\\"key\\\":\\\"datastore.couchbase.DML_error\\\",\\\"message\\\":\\\"DML Error, possible causes include concurrent modification. Failed to perform INSERT on key k10\\\",\\\"retry\\\":false}\",\r\n\u00a0\u00a0\u00a0\u00a0\"stack\": \"Error\\n\u00a0 \u00a0 at doInsert (functions\/n1ql.js:3:17)\"\r\n\u00a0\u00a0}\r\n]<\/pre>\n<p><span style=\"font-weight: 400\">If you want to obtain a nice returned value, you have to parse the <em>error.message<\/em> object before returning it:<\/span><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-13695\" src=\"https:\/\/blog.couchbase.com\/wp-content\/uploads\/2022\/08\/image_2022-08-25_141540752.png\" alt=\"\" width=\"969\" height=\"257\" \/><\/p>\n<p><span style=\"font-weight: 400\">Which yields:<\/span><\/p>\n<pre class=\"nums:false lang:js decode:true\">[\r\n\u00a0\u00a0{\r\n\u00a0\u00a0\u00a0\u00a0\"caller\": \"couchbase:2088\",\r\n\u00a0\u00a0\u00a0\u00a0\"cause\": {\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\"caller\": \"couchbase:1961\",\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\"code\": 17012,\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\"key\": \"dml.statement.duplicatekey\",\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\"message\": \"Duplicate Key: k10\"\r\n\u00a0\u00a0\u00a0\u00a0},\r\n\u00a0\u00a0\u00a0\u00a0\"code\": 12009,\r\n\u00a0\u00a0\u00a0\u00a0\"icause\": \"Duplicate Key: k10\",\r\n\u00a0\u00a0\u00a0\u00a0\"key\": \"datastore.couchbase.DML_error\",\r\n\u00a0\u00a0\u00a0\u00a0\"message\": \"DML Error, possible causes include concurrent modification. Failed to perform INSERT on key k10\",\r\n\u00a0\u00a0\u00a0\u00a0\"retry\": false\r\n\u00a0\u00a0}\r\n]<\/pre>\n<p><span style=\"font-weight: 400\">And if you want to include the stack too, you have to construct a new object like so:<\/span><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-13696\" src=\"https:\/\/www.couchbase.com\/blog\/wp-content\/uploads\/2022\/08\/image_2022-08-25_141652514.png\" alt=\"\" width=\"869\" height=\"260\" \/><\/p>\n<p>W<span style=\"font-weight: 400\">hich yields:<\/span><\/p>\n<pre class=\"nums:false lang:js decode:true\">[\r\n\u00a0\u00a0{\r\n\u00a0\u00a0\u00a0\u00a0\"caller\": \"couchbase:2088\",\r\n\u00a0\u00a0\u00a0\u00a0\"cause\": {\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\"caller\": \"couchbase:1961\",\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\"code\": 17012,\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\"key\": \"dml.statement.duplicatekey\",\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\"message\": \"Duplicate Key: k10\"\r\n\u00a0\u00a0\u00a0\u00a0},\r\n\u00a0\u00a0\u00a0\u00a0\"code\": 12009,\r\n\u00a0\u00a0\u00a0\u00a0\"icause\": \"Duplicate Key: k10\",\r\n\u00a0\u00a0\u00a0\u00a0\"key\": \"datastore.couchbase.DML_error\",\r\n\u00a0\u00a0\u00a0\u00a0\"message\": \"DML Error, possible causes include concurrent modification. Failed to perform INSERT on key k10\",\r\n\u00a0\u00a0\u00a0\u00a0\"retry\": false,\r\n\u00a0\u00a0\u00a0\u00a0\"stack\": \"Error\\n\u00a0 \u00a0 at doInsert (functions\/n1ql.js:3:17)\"\r\n\u00a0\u00a0}\r\n]<\/pre>\n<p><span style=\"font-weight: 400\">The moral of the story is: be mindful of how you return your error, and be mindful how you format it.<\/span><\/p>\n<h2><span style=\"font-weight: 400\">Conclusion<\/span><\/h2>\n<p><span style=\"font-weight: 400\">Over the course of several blogs, we have covered executing N1QL, processing documents and manipulating data, and handling errors.<\/span><\/p>\n<p><span style=\"font-weight: 400\">Next in the series, we&#8217;ll move to prepared statements.<\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>In previous blogs, we have covered executing N1QL from JavaScript functions, processing documents through iterators, and manipulating data. We now move onto handling errors from N1QL statements. Error handling When an error of any nature is encountered, the jsevaluator by [&hellip;]<\/p>\n","protected":false},"author":1782,"featured_media":13716,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"inline_featured_image":false,"footnotes":""},"categories":[1814,1816,2273,1812,2201],"tags":[1316,2133,8911],"ppma_author":[8924],"class_list":["post-13713","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-application-design","category-couchbase-server","category-eventing","category-n1ql-query","category-tools-sdks","tag-error-handling","tag-functions","tag-udf"],"acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO Premium plugin v25.9 (Yoast SEO v25.9) - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>From N1QL to JavaScript and back - Part 4: Error Handling - The Couchbase Blog<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.couchbase.com\/blog\/from-n1ql-to-javascript-and-back-part-4\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"From N1QL to JavaScript and back - Part 4: Error Handling\" \/>\n<meta property=\"og:description\" content=\"In previous blogs, we have covered executing N1QL from JavaScript functions, processing documents through iterators, and manipulating data. We now move onto handling errors from N1QL statements. Error handling When an error of any nature is encountered, the jsevaluator by [&hellip;]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.couchbase.com\/blog\/from-n1ql-to-javascript-and-back-part-4\/\" \/>\n<meta property=\"og:site_name\" content=\"The Couchbase Blog\" \/>\n<meta property=\"article:published_time\" content=\"2022-08-25T20:18:18+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2025-06-14T00:32:00+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.couchbase.com\/blog\/wp-content\/uploads\/sites\/1\/2022\/08\/n1ql-error-handling-couchbase_sml.png\" \/>\n\t<meta property=\"og:image:width\" content=\"1200\" \/>\n\t<meta property=\"og:image:height\" content=\"800\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"Marco Greco, Software Architect, Couchbase\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Marco Greco, Software Architect, Couchbase\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"4 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/www.couchbase.com\/blog\/from-n1ql-to-javascript-and-back-part-4\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/www.couchbase.com\/blog\/from-n1ql-to-javascript-and-back-part-4\/\"},\"author\":{\"name\":\"Marco Greco, Software Architect, Couchbase\",\"@id\":\"https:\/\/www.couchbase.com\/blog\/#\/schema\/person\/2b5184a7cdb443ff2897aff0866cd6fd\"},\"headline\":\"From N1QL to JavaScript and back &#8211; Part 4: Error Handling\",\"datePublished\":\"2022-08-25T20:18:18+00:00\",\"dateModified\":\"2025-06-14T00:32:00+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/www.couchbase.com\/blog\/from-n1ql-to-javascript-and-back-part-4\/\"},\"wordCount\":449,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/www.couchbase.com\/blog\/#organization\"},\"image\":{\"@id\":\"https:\/\/www.couchbase.com\/blog\/from-n1ql-to-javascript-and-back-part-4\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/www.couchbase.com\/blog\/wp-content\/uploads\/sites\/1\/2022\/08\/n1ql-error-handling-couchbase_sml.png\",\"keywords\":[\"Error handling\",\"functions\",\"User Defined Function (UDF)\"],\"articleSection\":[\"Application Design\",\"Couchbase Server\",\"Eventing\",\"SQL++ \/ N1QL Query\",\"Tools &amp; SDKs\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/www.couchbase.com\/blog\/from-n1ql-to-javascript-and-back-part-4\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.couchbase.com\/blog\/from-n1ql-to-javascript-and-back-part-4\/\",\"url\":\"https:\/\/www.couchbase.com\/blog\/from-n1ql-to-javascript-and-back-part-4\/\",\"name\":\"From N1QL to JavaScript and back - Part 4: Error Handling - The Couchbase Blog\",\"isPartOf\":{\"@id\":\"https:\/\/www.couchbase.com\/blog\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/www.couchbase.com\/blog\/from-n1ql-to-javascript-and-back-part-4\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/www.couchbase.com\/blog\/from-n1ql-to-javascript-and-back-part-4\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/www.couchbase.com\/blog\/wp-content\/uploads\/sites\/1\/2022\/08\/n1ql-error-handling-couchbase_sml.png\",\"datePublished\":\"2022-08-25T20:18:18+00:00\",\"dateModified\":\"2025-06-14T00:32:00+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/www.couchbase.com\/blog\/from-n1ql-to-javascript-and-back-part-4\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.couchbase.com\/blog\/from-n1ql-to-javascript-and-back-part-4\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.couchbase.com\/blog\/from-n1ql-to-javascript-and-back-part-4\/#primaryimage\",\"url\":\"https:\/\/www.couchbase.com\/blog\/wp-content\/uploads\/sites\/1\/2022\/08\/n1ql-error-handling-couchbase_sml.png\",\"contentUrl\":\"https:\/\/www.couchbase.com\/blog\/wp-content\/uploads\/sites\/1\/2022\/08\/n1ql-error-handling-couchbase_sml.png\",\"width\":1200,\"height\":800,\"caption\":\"N1QL error handling Couchbase\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.couchbase.com\/blog\/from-n1ql-to-javascript-and-back-part-4\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.couchbase.com\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"From N1QL to JavaScript and back &#8211; Part 4: Error Handling\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.couchbase.com\/blog\/#website\",\"url\":\"https:\/\/www.couchbase.com\/blog\/\",\"name\":\"The Couchbase Blog\",\"description\":\"Couchbase, the NoSQL Database\",\"publisher\":{\"@id\":\"https:\/\/www.couchbase.com\/blog\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.couchbase.com\/blog\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/www.couchbase.com\/blog\/#organization\",\"name\":\"The Couchbase Blog\",\"url\":\"https:\/\/www.couchbase.com\/blog\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.couchbase.com\/blog\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/www.couchbase.com\/blog\/wp-content\/uploads\/2023\/04\/admin-logo.png\",\"contentUrl\":\"https:\/\/www.couchbase.com\/blog\/wp-content\/uploads\/2023\/04\/admin-logo.png\",\"width\":218,\"height\":34,\"caption\":\"The Couchbase Blog\"},\"image\":{\"@id\":\"https:\/\/www.couchbase.com\/blog\/#\/schema\/logo\/image\/\"}},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.couchbase.com\/blog\/#\/schema\/person\/2b5184a7cdb443ff2897aff0866cd6fd\",\"name\":\"Marco Greco, Software Architect, Couchbase\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.couchbase.com\/blog\/#\/schema\/person\/image\/707c967b795fd71b6330f6d3118cf308\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/e6be0ee56851d2f71a554731d5edd5c820069680f0a810b47f094091c58bc553?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/e6be0ee56851d2f71a554731d5edd5c820069680f0a810b47f094091c58bc553?s=96&d=mm&r=g\",\"caption\":\"Marco Greco, Software Architect, Couchbase\"},\"description\":\"In a previous life, Marco used to be CTO, radiation physicist, software architect, sysadmin, DBA, trainer and general handyman at Italy's largest radiation theraphy practice. Having switched career and country, he spent more than two decades in various support and development positions in Informix first and IBM later, before finally taking the plunge and joining Couchbase, to help them make gold out of N1QL. He holds several patents and has authored open source projects of his own.\",\"sameAs\":[\"https:\/\/github.com\/marcogrecopriolo\",\"https:\/\/www.linkedin.com\/in\/marco-greco-7665308\/\"],\"url\":\"https:\/\/www.couchbase.com\/blog\/author\/marcocouchbase-com\/\"}]}<\/script>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"From N1QL to JavaScript and back - Part 4: Error Handling - The Couchbase Blog","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.couchbase.com\/blog\/from-n1ql-to-javascript-and-back-part-4\/","og_locale":"en_US","og_type":"article","og_title":"From N1QL to JavaScript and back - Part 4: Error Handling","og_description":"In previous blogs, we have covered executing N1QL from JavaScript functions, processing documents through iterators, and manipulating data. We now move onto handling errors from N1QL statements. Error handling When an error of any nature is encountered, the jsevaluator by [&hellip;]","og_url":"https:\/\/www.couchbase.com\/blog\/from-n1ql-to-javascript-and-back-part-4\/","og_site_name":"The Couchbase Blog","article_published_time":"2022-08-25T20:18:18+00:00","article_modified_time":"2025-06-14T00:32:00+00:00","og_image":[{"width":1200,"height":800,"url":"https:\/\/www.couchbase.com\/blog\/wp-content\/uploads\/sites\/1\/2022\/08\/n1ql-error-handling-couchbase_sml.png","type":"image\/png"}],"author":"Marco Greco, Software Architect, Couchbase","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Marco Greco, Software Architect, Couchbase","Est. reading time":"4 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.couchbase.com\/blog\/from-n1ql-to-javascript-and-back-part-4\/#article","isPartOf":{"@id":"https:\/\/www.couchbase.com\/blog\/from-n1ql-to-javascript-and-back-part-4\/"},"author":{"name":"Marco Greco, Software Architect, Couchbase","@id":"https:\/\/www.couchbase.com\/blog\/#\/schema\/person\/2b5184a7cdb443ff2897aff0866cd6fd"},"headline":"From N1QL to JavaScript and back &#8211; Part 4: Error Handling","datePublished":"2022-08-25T20:18:18+00:00","dateModified":"2025-06-14T00:32:00+00:00","mainEntityOfPage":{"@id":"https:\/\/www.couchbase.com\/blog\/from-n1ql-to-javascript-and-back-part-4\/"},"wordCount":449,"commentCount":0,"publisher":{"@id":"https:\/\/www.couchbase.com\/blog\/#organization"},"image":{"@id":"https:\/\/www.couchbase.com\/blog\/from-n1ql-to-javascript-and-back-part-4\/#primaryimage"},"thumbnailUrl":"https:\/\/www.couchbase.com\/blog\/wp-content\/uploads\/sites\/1\/2022\/08\/n1ql-error-handling-couchbase_sml.png","keywords":["Error handling","functions","User Defined Function (UDF)"],"articleSection":["Application Design","Couchbase Server","Eventing","SQL++ \/ N1QL Query","Tools &amp; SDKs"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.couchbase.com\/blog\/from-n1ql-to-javascript-and-back-part-4\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.couchbase.com\/blog\/from-n1ql-to-javascript-and-back-part-4\/","url":"https:\/\/www.couchbase.com\/blog\/from-n1ql-to-javascript-and-back-part-4\/","name":"From N1QL to JavaScript and back - Part 4: Error Handling - The Couchbase Blog","isPartOf":{"@id":"https:\/\/www.couchbase.com\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.couchbase.com\/blog\/from-n1ql-to-javascript-and-back-part-4\/#primaryimage"},"image":{"@id":"https:\/\/www.couchbase.com\/blog\/from-n1ql-to-javascript-and-back-part-4\/#primaryimage"},"thumbnailUrl":"https:\/\/www.couchbase.com\/blog\/wp-content\/uploads\/sites\/1\/2022\/08\/n1ql-error-handling-couchbase_sml.png","datePublished":"2022-08-25T20:18:18+00:00","dateModified":"2025-06-14T00:32:00+00:00","breadcrumb":{"@id":"https:\/\/www.couchbase.com\/blog\/from-n1ql-to-javascript-and-back-part-4\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.couchbase.com\/blog\/from-n1ql-to-javascript-and-back-part-4\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.couchbase.com\/blog\/from-n1ql-to-javascript-and-back-part-4\/#primaryimage","url":"https:\/\/www.couchbase.com\/blog\/wp-content\/uploads\/sites\/1\/2022\/08\/n1ql-error-handling-couchbase_sml.png","contentUrl":"https:\/\/www.couchbase.com\/blog\/wp-content\/uploads\/sites\/1\/2022\/08\/n1ql-error-handling-couchbase_sml.png","width":1200,"height":800,"caption":"N1QL error handling Couchbase"},{"@type":"BreadcrumbList","@id":"https:\/\/www.couchbase.com\/blog\/from-n1ql-to-javascript-and-back-part-4\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.couchbase.com\/blog\/"},{"@type":"ListItem","position":2,"name":"From N1QL to JavaScript and back &#8211; Part 4: Error Handling"}]},{"@type":"WebSite","@id":"https:\/\/www.couchbase.com\/blog\/#website","url":"https:\/\/www.couchbase.com\/blog\/","name":"The Couchbase Blog","description":"Couchbase, the NoSQL Database","publisher":{"@id":"https:\/\/www.couchbase.com\/blog\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.couchbase.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.couchbase.com\/blog\/#organization","name":"The Couchbase Blog","url":"https:\/\/www.couchbase.com\/blog\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.couchbase.com\/blog\/#\/schema\/logo\/image\/","url":"https:\/\/www.couchbase.com\/blog\/wp-content\/uploads\/2023\/04\/admin-logo.png","contentUrl":"https:\/\/www.couchbase.com\/blog\/wp-content\/uploads\/2023\/04\/admin-logo.png","width":218,"height":34,"caption":"The Couchbase Blog"},"image":{"@id":"https:\/\/www.couchbase.com\/blog\/#\/schema\/logo\/image\/"}},{"@type":"Person","@id":"https:\/\/www.couchbase.com\/blog\/#\/schema\/person\/2b5184a7cdb443ff2897aff0866cd6fd","name":"Marco Greco, Software Architect, Couchbase","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.couchbase.com\/blog\/#\/schema\/person\/image\/707c967b795fd71b6330f6d3118cf308","url":"https:\/\/secure.gravatar.com\/avatar\/e6be0ee56851d2f71a554731d5edd5c820069680f0a810b47f094091c58bc553?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/e6be0ee56851d2f71a554731d5edd5c820069680f0a810b47f094091c58bc553?s=96&d=mm&r=g","caption":"Marco Greco, Software Architect, Couchbase"},"description":"In a previous life, Marco used to be CTO, radiation physicist, software architect, sysadmin, DBA, trainer and general handyman at Italy's largest radiation theraphy practice. Having switched career and country, he spent more than two decades in various support and development positions in Informix first and IBM later, before finally taking the plunge and joining Couchbase, to help them make gold out of N1QL. He holds several patents and has authored open source projects of his own.","sameAs":["https:\/\/github.com\/marcogrecopriolo","https:\/\/www.linkedin.com\/in\/marco-greco-7665308\/"],"url":"https:\/\/www.couchbase.com\/blog\/author\/marcocouchbase-com\/"}]}},"authors":[{"term_id":8924,"user_id":1782,"is_guest":0,"slug":"marcocouchbase-com","display_name":"Marco Greco, Software Architect, Couchbase","avatar_url":"https:\/\/secure.gravatar.com\/avatar\/e6be0ee56851d2f71a554731d5edd5c820069680f0a810b47f094091c58bc553?s=96&d=mm&r=g","author_category":"","last_name":"Greco","first_name":"Marco","job_title":"","user_url":"","description":"In a previous life, Marco used to be CTO, radiation physicist, software architect, sysadmin, DBA, trainer and general handyman at Italy's largest radiation theraphy practice.\r\n\r\nHaving switched career and country, he spent more than two decades in various support and development positions in Informix first and IBM later, before finally taking the plunge and joining Couchbase, to help them make gold out of N1QL.\r\n\r\nHe holds several patents and has authored open source projects of his own."}],"_links":{"self":[{"href":"https:\/\/www.couchbase.com\/blog\/wp-json\/wp\/v2\/posts\/13713","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.couchbase.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.couchbase.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.couchbase.com\/blog\/wp-json\/wp\/v2\/users\/1782"}],"replies":[{"embeddable":true,"href":"https:\/\/www.couchbase.com\/blog\/wp-json\/wp\/v2\/comments?post=13713"}],"version-history":[{"count":0,"href":"https:\/\/www.couchbase.com\/blog\/wp-json\/wp\/v2\/posts\/13713\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.couchbase.com\/blog\/wp-json\/wp\/v2\/media\/13716"}],"wp:attachment":[{"href":"https:\/\/www.couchbase.com\/blog\/wp-json\/wp\/v2\/media?parent=13713"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.couchbase.com\/blog\/wp-json\/wp\/v2\/categories?post=13713"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.couchbase.com\/blog\/wp-json\/wp\/v2\/tags?post=13713"},{"taxonomy":"author","embeddable":true,"href":"https:\/\/www.couchbase.com\/blog\/wp-json\/wp\/v2\/ppma_author?post=13713"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}