diff --git a/datastore/error.js b/datastore/error.js new file mode 100644 index 0000000000..b03621c457 --- /dev/null +++ b/datastore/error.js @@ -0,0 +1,55 @@ +// Copyright 2016, 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'; + +var gcloud = require('gcloud'); + +var dataset = gcloud.datastore.dataset({ + projectId: process.env.GCLOUD_PROJECT +}); + +// [START error] +function runQuery(cb) { + var query = dataset.createQuery(['foo']).start('badrequest'); + + dataset.runQuery(query, function (err, entities) { + // Check for an error + if (err) { + console.log(err.errors); // [...] + console.log(err.code); // 400 + console.log(err.message); // "Bad Request" + console.log(err.response); // {...} + + // Process error + + // For example, treat permission error like no entities were found + if (err.code === 403) { + return cb(null, []); + } + + // Forward the error to the caller + return cb(err); + } + + // We're good + return cb(null, entities); + }); +} +// [END error] + +exports.runQuery = runQuery; + +if (module === require.main) { + runQuery(function () {}); +} diff --git a/datastore/package.json b/datastore/package.json index 1c99ffb783..8a0a7958a7 100644 --- a/datastore/package.json +++ b/datastore/package.json @@ -9,6 +9,7 @@ "node": ">=0.10.x" }, "scripts": { + "error": "node error.js", "tasks": "node tasks.js" }, "dependencies": { diff --git a/test/datastore/error.test.js b/test/datastore/error.test.js new file mode 100644 index 0000000000..26cf0cb1c6 --- /dev/null +++ b/test/datastore/error.test.js @@ -0,0 +1,31 @@ +// Copyright 2015-2016, 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'; + +var error = require('../../datastore/error.js'); +var assert = require('assert'); + +describe('datastore/error', function () { + it('should have an error', function (done) { + error.runQuery(function (err) { + try { + assert.ok(err); + assert.equal(err.code, 400); + done(); + } catch (err) { + done(err); + } + }); + }); +});