forked from googleapis/nodejs-bigtable
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patherrors.ts
149 lines (143 loc) · 4.88 KB
/
errors.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
// Copyright 2022 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
//
// 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.
//
// ** This file is automatically generated by gapic-generator-typescript. **
// ** https://github.com/googleapis/gapic-generator-typescript **
// ** All changes to this file may be overwritten. **
import {before, describe, it} from 'mocha';
import {Bigtable} from '../src';
import * as assert from 'assert';
import {GoogleError, grpc, ServiceError} from 'google-gax';
import {MockServer} from '../src/util/mock-servers/mock-server';
import {BigtableClientMockService} from '../src/util/mock-servers/service-implementations/bigtable-client-mock-service';
import {MockService} from '../src/util/mock-servers/mock-service';
function isServiceError(error: any): error is ServiceError {
return (
error.code !== undefined &&
error.details !== undefined &&
error.metadata !== undefined
);
}
describe('Bigtable/Errors', () => {
let server: MockServer;
let bigtable: Bigtable;
let table: any;
let service: MockService;
before(async () => {
// make sure we have everything initialized before starting tests
const port = await new Promise<string>(resolve => {
server = new MockServer(resolve);
});
bigtable = new Bigtable({
apiEndpoint: `localhost:${port}`,
});
service = new BigtableClientMockService(server);
table = bigtable.instance('fake-instance').table('fake-table');
});
describe('with the bigtable data client', () => {
describe('sends errors through a streaming request', () => {
const errorDetails =
'Table not found: projects/my-project/instances/my-instance/tables/my-table';
const emitTableNotExistsError = (stream: any) => {
// TODO: Replace stream with type
const metadata = new grpc.Metadata();
metadata.set(
'grpc-server-stats-bin',
Buffer.from([0, 0, 116, 73, 159, 3, 0, 0, 0, 0])
);
stream.emit('error', {
code: 5,
details: errorDetails,
metadata,
});
};
function checkTableNotExistError(err: any) {
if (isServiceError(err)) {
const {code, message, details} = err;
assert.strictEqual(details, errorDetails);
assert.strictEqual(code, 5);
assert.strictEqual(message, `5 NOT_FOUND: ${errorDetails}`);
} else {
assert.fail(
'Errors checked using this function should all be GoogleErrors'
);
}
}
describe('with ReadRows service', () => {
before(async () => {
service.setService({
ReadRows: emitTableNotExistsError,
});
});
it('should produce human readable error when passing through gax', done => {
const readStream = table.createReadStream({});
readStream.on('error', (err: GoogleError) => {
checkTableNotExistError(err);
done();
});
});
});
describe('with mutateRows service through insert', () => {
before(async () => {
service.setService({
mutateRows: emitTableNotExistsError,
});
});
it('should produce human readable error when passing through gax', async () => {
const timestamp = new Date();
const rowsToInsert = [
{
key: 'r2',
data: {
cf1: {
c1: {
value: 'test-value2',
labels: [],
timestamp,
},
},
},
},
];
try {
await table.insert(rowsToInsert);
} catch (err) {
checkTableNotExistError(err);
return;
}
assert.fail('An error should have been thrown by the stream');
});
});
describe('with sampleRowKeys', () => {
before(async () => {
service.setService({
sampleRowKeys: emitTableNotExistsError,
});
});
it('should produce human readable error when passing through gax', async () => {
try {
await table.sampleRowKeys({});
} catch (err) {
checkTableNotExistError(err);
return;
}
assert.fail('An error should have been thrown by the stream');
});
});
});
});
after(async () => {
server.shutdown(() => {});
});
});