|
| 1 | +// Copyright 2016, Google, Inc. |
| 2 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 3 | +// you may not use this file except in compliance with the License. |
| 4 | +// You may obtain a copy of the License at |
| 5 | +// |
| 6 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 7 | +// |
| 8 | +// Unless required by applicable law or agreed to in writing, software |
| 9 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 10 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 | +// See the License for the specific language governing permissions and |
| 12 | +// limitations under the License. |
| 13 | + |
| 14 | +'use strict'; |
| 15 | + |
| 16 | +var gcloud = require('gcloud'); |
| 17 | + |
| 18 | +var dataset = gcloud.datastore.dataset({ |
| 19 | + projectId: process.env.GCLOUD_PROJECT |
| 20 | +}); |
| 21 | + |
| 22 | +// [START error] |
| 23 | +function runQuery(cb) { |
| 24 | + var query = dataset.createQuery(['foo']).start('badrequest'); |
| 25 | + |
| 26 | + dataset.runQuery(query, function (err, entities) { |
| 27 | + // Check for an error |
| 28 | + if (err) { |
| 29 | + console.log(err.errors); // [...] |
| 30 | + console.log(err.code); // 400 |
| 31 | + console.log(err.message); // "Bad Request" |
| 32 | + console.log(err.response); // {...} |
| 33 | + |
| 34 | + // Process error |
| 35 | + |
| 36 | + // For example, treat permission error like no entities were found |
| 37 | + if (err.code === 403) { |
| 38 | + return cb(null, []); |
| 39 | + } |
| 40 | + |
| 41 | + // Forward the error to the caller |
| 42 | + return cb(err); |
| 43 | + } |
| 44 | + |
| 45 | + // We're good |
| 46 | + return cb(null, entities); |
| 47 | + }); |
| 48 | +} |
| 49 | +// [END error] |
| 50 | + |
| 51 | +exports.runQuery = runQuery; |
| 52 | + |
| 53 | +if (module === require.main) { |
| 54 | + runQuery(function () {}); |
| 55 | +} |
0 commit comments