Skip to content

Commit

Permalink
Merge pull request #66 from GoogleCloudPlatform/error
Browse files Browse the repository at this point in the history
Added Datastore error sample.
  • Loading branch information
jmdobry committed Feb 11, 2016
2 parents af80361 + ead6bfc commit 90854b5
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 0 deletions.
55 changes: 55 additions & 0 deletions datastore/error.js
Original file line number Diff line number Diff line change
@@ -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 () {});
}
1 change: 1 addition & 0 deletions datastore/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
"node": ">=0.10.x"
},
"scripts": {
"error": "node error.js",
"tasks": "node tasks.js"
},
"dependencies": {
Expand Down
31 changes: 31 additions & 0 deletions test/datastore/error.test.js
Original file line number Diff line number Diff line change
@@ -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);
}
});
});
});

0 comments on commit 90854b5

Please sign in to comment.