From 055eeeff14f6afdaf0bd5a16f839828a001a3e72 Mon Sep 17 00:00:00 2001 From: vishald123 <44579906+vishald123@users.noreply.github.com> Date: Tue, 30 Jul 2019 04:31:58 -0400 Subject: [PATCH 01/18] Add error sample --- samples/error.js | 64 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) create mode 100644 samples/error.js diff --git a/samples/error.js b/samples/error.js new file mode 100644 index 000000000..aaac6dbcd --- /dev/null +++ b/samples/error.js @@ -0,0 +1,64 @@ +/** + * Copyright 2018, Google, Inc. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * @see [Cloud Datastore Concepts errors] {@link https://cloud.google.com/datastore/docs/concepts/errors} + * for more information on errors + */ + +'use strict'; + +const {Datastore} = require('@google-cloud/datastore'); + +// [START error] +function runQuery() { + // Creates a client + const datastore = new Datastore(); + + const query = datastore.createQuery(['Company']).start('badrequest'); + + return datastore + .runQuery(query) + .then(results => { + const entities = results[0]; + console.log('Entities:'); + entities.forEach(entity => console.log(entity)); + return entities; + }) + .catch(err => { + console.log(err.code); //400 + console.log(err.message); //"Key path is incomplete: [Person: null]" + console.log(err.status); //"INVALID_ARGUMENT" + /** + * @see [For more information on error codes refer] https://cloud.google.com/datastore/docs/concepts/errors#error_codes + */ + + // Process error + + // For example, treat permission error like no entities were found + // eslint-disable-next-line no-constant-condition + if (/* some condition */ false) { + return []; + } + + //Forward the error to caller + return Promise.reject(err); + }); +} +// [END error] + +exports.runQuery = runQuery; + +if (module === require.main) { + exports.runQuery(); +} From 0621d0bb2734b4b4f35e7af76a55ca58d63d325a Mon Sep 17 00:00:00 2001 From: vishald123 <44579906+vishald123@users.noreply.github.com> Date: Tue, 30 Jul 2019 04:33:05 -0400 Subject: [PATCH 02/18] test sample --- samples/test/error.test.js | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 samples/test/error.test.js diff --git a/samples/test/error.test.js b/samples/test/error.test.js new file mode 100644 index 000000000..4ffb145ae --- /dev/null +++ b/samples/test/error.test.js @@ -0,0 +1,32 @@ +/** + * Copyright 2018, Google, Inc. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ +'use strict'; + +const error = require('../error'); +const assert = require('assert'); + +describe('error', () => { + it('should have an error', done => { + const errorCode = 3; + error.runQuery().then( + () => {}, + err => { + assert.strictEqual(err.code, errorCode); + done(); + } + ); + }); +}); From 87d30a54134675e748662a44ffa3cfba47736328 Mon Sep 17 00:00:00 2001 From: vishald123 <44579906+vishald123@users.noreply.github.com> Date: Wed, 31 Jul 2019 12:17:56 -0400 Subject: [PATCH 03/18] lint_fix --- samples/error.js | 62 ++++++++++++++++++++++++------------------------ 1 file changed, 31 insertions(+), 31 deletions(-) diff --git a/samples/error.js b/samples/error.js index aaac6dbcd..a26dee3e2 100644 --- a/samples/error.js +++ b/samples/error.js @@ -11,54 +11,54 @@ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. - * + * * @see [Cloud Datastore Concepts errors] {@link https://cloud.google.com/datastore/docs/concepts/errors} * for more information on errors - */ + */ -'use strict'; +'use strict'; -const {Datastore} = require('@google-cloud/datastore'); +const {Datastore} = require('@google-cloud/datastore'); -// [START error] -function runQuery() { - // Creates a client - const datastore = new Datastore(); +// [START error] +function runQuery() { + // Creates a client + const datastore = new Datastore(); - const query = datastore.createQuery(['Company']).start('badrequest'); + const query = datastore.createQuery(['Company']).start('badrequest'); - return datastore - .runQuery(query) - .then(results => { - const entities = results[0]; - console.log('Entities:'); - entities.forEach(entity => console.log(entity)); - return entities; - }) + return datastore + .runQuery(query) + .then(results => { + const entities = results[0]; + console.log('Entities:'); + entities.forEach(entity => console.log(entity)); + return entities; + }) .catch(err => { console.log(err.code); //400 console.log(err.message); //"Key path is incomplete: [Person: null]" console.log(err.status); //"INVALID_ARGUMENT" /** - * @see [For more information on error codes refer] https://cloud.google.com/datastore/docs/concepts/errors#error_codes - */ + * @see [For more information on error codes refer] https://cloud.google.com/datastore/docs/concepts/errors#error_codes + */ // Process error - // For example, treat permission error like no entities were found + // For example, treat permission error like no entities were found // eslint-disable-next-line no-constant-condition - if (/* some condition */ false) { - return []; - } - + if (/* some condition */ false) { + return []; + } + //Forward the error to caller return Promise.reject(err); - }); -} -// [END error] + }); +} +// [END error] -exports.runQuery = runQuery; +exports.runQuery = runQuery; -if (module === require.main) { - exports.runQuery(); -} +if (module === require.main) { + exports.runQuery(); +} From c75e083c6ca9d1bcfed4dd0eab1fb0e8630c1d4f Mon Sep 17 00:00:00 2001 From: Alex Date: Fri, 2 Aug 2019 11:17:38 -0400 Subject: [PATCH 04/18] lint --- samples/test/error.test.js | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/samples/test/error.test.js b/samples/test/error.test.js index 4ffb145ae..a47892c4a 100644 --- a/samples/test/error.test.js +++ b/samples/test/error.test.js @@ -11,22 +11,22 @@ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. - * + * */ -'use strict'; +'use strict'; const error = require('../error'); const assert = require('assert'); describe('error', () => { - it('should have an error', done => { - const errorCode = 3; - error.runQuery().then( - () => {}, - err => { - assert.strictEqual(err.code, errorCode); - done(); - } - ); - }); + it('should have an error', done => { + const errorCode = 3; + error.runQuery().then( + () => {}, + err => { + assert.strictEqual(err.code, errorCode); + done(); + } + ); + }); }); From 93a07760044806d5bd988bf1b818a76b11fc7686 Mon Sep 17 00:00:00 2001 From: vishald123 <44579906+vishald123@users.noreply.github.com> Date: Tue, 20 Aug 2019 05:58:52 -0400 Subject: [PATCH 05/18] Suggested changes --- samples/error.js | 71 +++++++++++++++++++++++++++--------------------- 1 file changed, 40 insertions(+), 31 deletions(-) diff --git a/samples/error.js b/samples/error.js index a26dee3e2..5fb5efa6c 100644 --- a/samples/error.js +++ b/samples/error.js @@ -18,47 +18,56 @@ 'use strict'; +// sample-metadata +// title: error sample +// description: sample show how to handle and process error +// usage: node error.js + const {Datastore} = require('@google-cloud/datastore'); // [START error] -function runQuery() { +function main() { // Creates a client const datastore = new Datastore(); const query = datastore.createQuery(['Company']).start('badrequest'); - return datastore - .runQuery(query) - .then(results => { - const entities = results[0]; - console.log('Entities:'); - entities.forEach(entity => console.log(entity)); - return entities; - }) - .catch(err => { - console.log(err.code); //400 - console.log(err.message); //"Key path is incomplete: [Person: null]" - console.log(err.status); //"INVALID_ARGUMENT" - /** - * @see [For more information on error codes refer] https://cloud.google.com/datastore/docs/concepts/errors#error_codes - */ - - // Process error + async function runQuery() { + return await datastore + .runQuery(query) + .then(results => { + const entities = results[0]; + console.log('Entities:'); + entities.forEach(entity => console.log(entity)); + return entities; + }) + .catch(err => { + // Get the error information + const code = err.code; + const message = err.message; + /** + * @see [For more information on error codes refer] https://cloud.google.com/datastore/docs/concepts/errors#error_codes + */ - // For example, treat permission error like no entities were found - // eslint-disable-next-line no-constant-condition - if (/* some condition */ false) { - return []; - } + // Process error - //Forward the error to caller - return Promise.reject(err); - }); -} -// [END error] + // For example, return a custom message to user + // based on the error code and or error message + // eslint-disable-next-line no-constant-condition + if (code === 4 && message === 'some message') { + err.message = 'Oops, something went wrong'; + } -exports.runQuery = runQuery; + //Forward the error to caller + throw err; + }); + } -if (module === require.main) { - exports.runQuery(); + runQuery().catch(err => { + console.log(err.code); // 3 + console.log(err.message); // "Error parsing protocol message" + }); + // [END error] } + +main(...process.argv.slice(2)); From cadcab47e0e2147c4f8f298c1203ae51b5fa66eb Mon Sep 17 00:00:00 2001 From: vishald123 <44579906+vishald123@users.noreply.github.com> Date: Tue, 20 Aug 2019 06:04:40 -0400 Subject: [PATCH 06/18] Suggested changes --- samples/test/error.test.js | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/samples/test/error.test.js b/samples/test/error.test.js index a47892c4a..2888fb75f 100644 --- a/samples/test/error.test.js +++ b/samples/test/error.test.js @@ -11,22 +11,21 @@ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. - * */ 'use strict'; -const error = require('../error'); const assert = require('assert'); +const {execSync} = require('child_process'); +const exec = cmd => + execSync(cmd, { + encoding: 'utf8', + }); describe('error', () => { it('should have an error', done => { const errorCode = 3; - error.runQuery().then( - () => {}, - err => { - assert.strictEqual(err.code, errorCode); - done(); - } - ); + const output = exec(`node error.js`); + assert.strictEqual(parseInt(output[0]), errorCode); + done(); }); }); From 54359b5aa5f71b19985460a954a919458e57a2a0 Mon Sep 17 00:00:00 2001 From: vishald123 <44579906+vishald123@users.noreply.github.com> Date: Tue, 20 Aug 2019 07:03:56 -0400 Subject: [PATCH 07/18] Suggested changes --- samples/error.js | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/samples/error.js b/samples/error.js index 5fb5efa6c..b7c2a7672 100644 --- a/samples/error.js +++ b/samples/error.js @@ -1,19 +1,17 @@ -/** - * Copyright 2018, Google, Inc. +/*! + * Copyright 2019 Google Inc. All Rights Reserved. + * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * - * http://www.apache.org/licenses/LICENSE-2.0 + * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. - * - * @see [Cloud Datastore Concepts errors] {@link https://cloud.google.com/datastore/docs/concepts/errors} - * for more information on errors */ 'use strict'; From 89c9159e08d42b12831f98db50a8c0cdbc64b207 Mon Sep 17 00:00:00 2001 From: vishald123 <44579906+vishald123@users.noreply.github.com> Date: Tue, 20 Aug 2019 09:27:15 -0400 Subject: [PATCH 08/18] Copyright changes --- samples/error.js | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/samples/error.js b/samples/error.js index b7c2a7672..5fb5efa6c 100644 --- a/samples/error.js +++ b/samples/error.js @@ -1,17 +1,19 @@ -/*! - * Copyright 2019 Google Inc. All Rights Reserved. - * +/** + * Copyright 2018, Google, Inc. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * - * http://www.apache.org/licenses/LICENSE-2.0 + * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. + * + * @see [Cloud Datastore Concepts errors] {@link https://cloud.google.com/datastore/docs/concepts/errors} + * for more information on errors */ 'use strict'; From 6ea8cd084995269ee9e68183d054e4acd589c58c Mon Sep 17 00:00:00 2001 From: vishald123 <44579906+vishald123@users.noreply.github.com> Date: Tue, 20 Aug 2019 09:32:09 -0400 Subject: [PATCH 09/18] Copyright changes --- samples/error.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/samples/error.js b/samples/error.js index 5fb5efa6c..7f04e2257 100644 --- a/samples/error.js +++ b/samples/error.js @@ -1,5 +1,5 @@ /** - * Copyright 2018, Google, Inc. + * Copyright 2018, Google LLC. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at From 4d84dde4888271b09e019d2f3bfa892b9b18ff82 Mon Sep 17 00:00:00 2001 From: vishald123 <44579906+vishald123@users.noreply.github.com> Date: Tue, 20 Aug 2019 09:32:55 -0400 Subject: [PATCH 10/18] Copyright changes --- samples/test/error.test.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/samples/test/error.test.js b/samples/test/error.test.js index 2888fb75f..4d7b7a7f2 100644 --- a/samples/test/error.test.js +++ b/samples/test/error.test.js @@ -1,5 +1,5 @@ /** - * Copyright 2018, Google, Inc. + * Copyright 2018, Google LLC. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at @@ -12,6 +12,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ + 'use strict'; const assert = require('assert'); From 8148a1b20bf237207d3166af346a349c197584be Mon Sep 17 00:00:00 2001 From: vishald123 <44579906+vishald123@users.noreply.github.com> Date: Tue, 20 Aug 2019 09:48:02 -0400 Subject: [PATCH 11/18] test --- samples/error.js | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/samples/error.js b/samples/error.js index 7f04e2257..0f515443a 100644 --- a/samples/error.js +++ b/samples/error.js @@ -1,19 +1,17 @@ /** - * Copyright 2018, Google LLC. + * Copyright 2019 Google LLC + * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * - * http://www.apache.org/licenses/LICENSE-2.0 + * https://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. - * - * @see [Cloud Datastore Concepts errors] {@link https://cloud.google.com/datastore/docs/concepts/errors} - * for more information on errors */ 'use strict'; @@ -23,7 +21,7 @@ // description: sample show how to handle and process error // usage: node error.js -const {Datastore} = require('@google-cloud/datastore'); +const { Datastore } = require('@google-cloud/datastore'); // [START error] function main() { From a0adc19578210b8cc76de68c66e74939608a055d Mon Sep 17 00:00:00 2001 From: vishald123 <44579906+vishald123@users.noreply.github.com> Date: Tue, 20 Aug 2019 09:48:52 -0400 Subject: [PATCH 12/18] test --- samples/test/error.test.js | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/samples/test/error.test.js b/samples/test/error.test.js index 4d7b7a7f2..1e71eb2fe 100644 --- a/samples/test/error.test.js +++ b/samples/test/error.test.js @@ -1,10 +1,11 @@ /** - * Copyright 2018, Google LLC. + * Copyright 2019 Google LLC + * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * - * http://www.apache.org/licenses/LICENSE-2.0 + * https://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, @@ -16,7 +17,7 @@ 'use strict'; const assert = require('assert'); -const {execSync} = require('child_process'); +const { execSync } = require('child_process'); const exec = cmd => execSync(cmd, { encoding: 'utf8', From fd586b6c7f33457b4f5f20e8a57e2d72fdc8131e Mon Sep 17 00:00:00 2001 From: vishald123 <44579906+vishald123@users.noreply.github.com> Date: Tue, 20 Aug 2019 10:02:09 -0400 Subject: [PATCH 13/18] test --- samples/error.js | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/samples/error.js b/samples/error.js index 0f515443a..5fb5efa6c 100644 --- a/samples/error.js +++ b/samples/error.js @@ -1,17 +1,19 @@ /** - * Copyright 2019 Google LLC - * + * Copyright 2018, Google, Inc. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * - * https://www.apache.org/licenses/LICENSE-2.0 + * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. + * + * @see [Cloud Datastore Concepts errors] {@link https://cloud.google.com/datastore/docs/concepts/errors} + * for more information on errors */ 'use strict'; @@ -21,7 +23,7 @@ // description: sample show how to handle and process error // usage: node error.js -const { Datastore } = require('@google-cloud/datastore'); +const {Datastore} = require('@google-cloud/datastore'); // [START error] function main() { From 76e5f324cf069ddc11749623e050aa6ae2b57426 Mon Sep 17 00:00:00 2001 From: vishald123 <44579906+vishald123@users.noreply.github.com> Date: Tue, 20 Aug 2019 10:02:50 -0400 Subject: [PATCH 14/18] test --- samples/test/error.test.js | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/samples/test/error.test.js b/samples/test/error.test.js index 1e71eb2fe..9967b1528 100644 --- a/samples/test/error.test.js +++ b/samples/test/error.test.js @@ -1,11 +1,10 @@ /** - * Copyright 2019 Google LLC - * + * Copyright 2018, Google, Inc. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * - * https://www.apache.org/licenses/LICENSE-2.0 + * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, @@ -17,7 +16,7 @@ 'use strict'; const assert = require('assert'); -const { execSync } = require('child_process'); +const {execSync} = require('child_process'); const exec = cmd => execSync(cmd, { encoding: 'utf8', From 5dd8d0dbbd4c8ca77bdc6c5321be7aceed04957e Mon Sep 17 00:00:00 2001 From: Alex Date: Tue, 20 Aug 2019 10:13:10 -0400 Subject: [PATCH 15/18] refactor: header --- samples/error.js | 6 ++---- samples/test/error.test.js | 3 ++- 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/samples/error.js b/samples/error.js index 5fb5efa6c..cfef43db4 100644 --- a/samples/error.js +++ b/samples/error.js @@ -1,5 +1,6 @@ /** - * Copyright 2018, Google, Inc. + * Copyright 2019 Google LLC. + * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at @@ -11,9 +12,6 @@ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. - * - * @see [Cloud Datastore Concepts errors] {@link https://cloud.google.com/datastore/docs/concepts/errors} - * for more information on errors */ 'use strict'; diff --git a/samples/test/error.test.js b/samples/test/error.test.js index 9967b1528..4e8c507e9 100644 --- a/samples/test/error.test.js +++ b/samples/test/error.test.js @@ -1,5 +1,6 @@ /** - * Copyright 2018, Google, Inc. + * Copyright 2019 Google LLC. + * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at From e141a7bf9f8d8e2b7dcec31cfb21a5b46c0a4d9a Mon Sep 17 00:00:00 2001 From: Alex Date: Tue, 20 Aug 2019 10:16:04 -0400 Subject: [PATCH 16/18] refactor: header2 --- samples/error.js | 4 ++-- samples/test/error.test.js | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/samples/error.js b/samples/error.js index cfef43db4..7b4027598 100644 --- a/samples/error.js +++ b/samples/error.js @@ -1,11 +1,11 @@ /** - * Copyright 2019 Google LLC. + * Copyright 2019 Google LLC * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * - * http://www.apache.org/licenses/LICENSE-2.0 + * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, diff --git a/samples/test/error.test.js b/samples/test/error.test.js index 4e8c507e9..928a08108 100644 --- a/samples/test/error.test.js +++ b/samples/test/error.test.js @@ -1,11 +1,11 @@ /** - * Copyright 2019 Google LLC. + * Copyright 2019 Google LLC * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * - * http://www.apache.org/licenses/LICENSE-2.0 + * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, From 90e6458ecb44f8d60a9b9abede96f2713ab2d914 Mon Sep 17 00:00:00 2001 From: Alex Date: Thu, 22 Aug 2019 12:31:23 -0400 Subject: [PATCH 17/18] fix: headers should pass now --- samples/error.js | 2 +- samples/test/error.test.js | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/samples/error.js b/samples/error.js index 7b4027598..166c0707b 100644 --- a/samples/error.js +++ b/samples/error.js @@ -1,5 +1,5 @@ /** - * Copyright 2019 Google LLC + * Copyright 2019 Google LLC. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. diff --git a/samples/test/error.test.js b/samples/test/error.test.js index 928a08108..87b9138cd 100644 --- a/samples/test/error.test.js +++ b/samples/test/error.test.js @@ -1,5 +1,5 @@ /** - * Copyright 2019 Google LLC + * Copyright 2019 Google LLC. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. From d0cad6356d3e98156dde7ccbcf6819fcf1221e46 Mon Sep 17 00:00:00 2001 From: Alex Date: Thu, 22 Aug 2019 16:47:02 -0400 Subject: [PATCH 18/18] refactor: simplify the example --- samples/error.js | 39 ++++++++++----------------------------- 1 file changed, 10 insertions(+), 29 deletions(-) diff --git a/samples/error.js b/samples/error.js index 166c0707b..78932567e 100644 --- a/samples/error.js +++ b/samples/error.js @@ -31,38 +31,19 @@ function main() { const query = datastore.createQuery(['Company']).start('badrequest'); async function runQuery() { - return await datastore - .runQuery(query) - .then(results => { - const entities = results[0]; - console.log('Entities:'); - entities.forEach(entity => console.log(entity)); - return entities; - }) - .catch(err => { - // Get the error information - const code = err.code; - const message = err.message; - /** - * @see [For more information on error codes refer] https://cloud.google.com/datastore/docs/concepts/errors#error_codes - */ - - // Process error - - // For example, return a custom message to user - // based on the error code and or error message - // eslint-disable-next-line no-constant-condition - if (code === 4 && message === 'some message') { - err.message = 'Oops, something went wrong'; - } - - //Forward the error to caller - throw err; - }); + try { + const [result] = await datastore.runQuery(query); + // etc., etc. + return result; + } catch (error) { + // do something with error. + console.log(error.code); // 3 + //Forward the error to caller + throw error; + } } runQuery().catch(err => { - console.log(err.code); // 3 console.log(err.message); // "Error parsing protocol message" }); // [END error]