Skip to content

Commit ead6bfc

Browse files
committed
Added Datastore error sample.
1 parent af80361 commit ead6bfc

File tree

3 files changed

+87
-0
lines changed

3 files changed

+87
-0
lines changed

datastore/error.js

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
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+
}

datastore/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
"node": ">=0.10.x"
1010
},
1111
"scripts": {
12+
"error": "node error.js",
1213
"tasks": "node tasks.js"
1314
},
1415
"dependencies": {

test/datastore/error.test.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// Copyright 2015-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 error = require('../../datastore/error.js');
17+
var assert = require('assert');
18+
19+
describe('datastore/error', function () {
20+
it('should have an error', function (done) {
21+
error.runQuery(function (err) {
22+
try {
23+
assert.ok(err);
24+
assert.equal(err.code, 400);
25+
done();
26+
} catch (err) {
27+
done(err);
28+
}
29+
});
30+
});
31+
});

0 commit comments

Comments
 (0)