|
| 1 | +// Copyright 2022 Google LLC |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// https://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | +// |
| 15 | +// ** This file is automatically generated by gapic-generator-typescript. ** |
| 16 | +// ** https://github.com/googleapis/gapic-generator-typescript ** |
| 17 | +// ** All changes to this file may be overwritten. ** |
| 18 | + |
| 19 | +import {before, describe, it} from 'mocha'; |
| 20 | +import {Bigtable} from '../src'; |
| 21 | +import * as assert from 'assert'; |
| 22 | + |
| 23 | +import {GoogleError, grpc, ServiceError} from 'google-gax'; |
| 24 | +import {MockServer} from '../src/util/mock-servers/mock-server'; |
| 25 | +import {BigtableClientMockService} from '../src/util/mock-servers/service-implementations/bigtable-client-mock-service'; |
| 26 | +import {MockService} from '../src/util/mock-servers/mock-service'; |
| 27 | + |
| 28 | +function isServiceError(error: any): error is ServiceError { |
| 29 | + return ( |
| 30 | + error.code !== undefined && |
| 31 | + error.details !== undefined && |
| 32 | + error.metadata !== undefined |
| 33 | + ); |
| 34 | +} |
| 35 | + |
| 36 | +describe('Bigtable/Errors', () => { |
| 37 | + let server: MockServer; |
| 38 | + let bigtable: Bigtable; |
| 39 | + let table: any; |
| 40 | + |
| 41 | + before(done => { |
| 42 | + server = new MockServer(() => { |
| 43 | + bigtable = new Bigtable({ |
| 44 | + apiEndpoint: `localhost:${server.port}`, |
| 45 | + }); |
| 46 | + table = bigtable.instance('fake-instance').table('fake-table'); |
| 47 | + done(); |
| 48 | + }); |
| 49 | + }); |
| 50 | + |
| 51 | + describe('with the bigtable data client', () => { |
| 52 | + let service: MockService; |
| 53 | + before(async () => { |
| 54 | + service = new BigtableClientMockService(server); |
| 55 | + }); |
| 56 | + |
| 57 | + describe('sends errors through a streaming request', () => { |
| 58 | + const errorDetails = |
| 59 | + 'Table not found: projects/my-project/instances/my-instance/tables/my-table'; |
| 60 | + const emitTableNotExistsError = (stream: any) => { |
| 61 | + // TODO: Replace stream with type |
| 62 | + const metadata = new grpc.Metadata(); |
| 63 | + metadata.set( |
| 64 | + 'grpc-server-stats-bin', |
| 65 | + Buffer.from([0, 0, 116, 73, 159, 3, 0, 0, 0, 0]) |
| 66 | + ); |
| 67 | + stream.emit('error', { |
| 68 | + code: 5, |
| 69 | + details: errorDetails, |
| 70 | + metadata, |
| 71 | + }); |
| 72 | + }; |
| 73 | + function checkTableNotExistError(err: any) { |
| 74 | + if (isServiceError(err)) { |
| 75 | + const {code, message, details} = err; |
| 76 | + assert.strictEqual(details, errorDetails); |
| 77 | + assert.strictEqual(code, 5); |
| 78 | + assert.strictEqual(message, `5 NOT_FOUND: ${errorDetails}`); |
| 79 | + } else { |
| 80 | + assert.fail( |
| 81 | + 'Errors checked using this function should all be GoogleErrors' |
| 82 | + ); |
| 83 | + } |
| 84 | + } |
| 85 | + describe('with ReadRows service', () => { |
| 86 | + before(async () => { |
| 87 | + service.setService({ |
| 88 | + ReadRows: emitTableNotExistsError, |
| 89 | + }); |
| 90 | + }); |
| 91 | + it('should produce human readable error when passing through gax', done => { |
| 92 | + const readStream = table.createReadStream({}); |
| 93 | + readStream.on('error', (err: GoogleError) => { |
| 94 | + checkTableNotExistError(err); |
| 95 | + done(); |
| 96 | + }); |
| 97 | + }); |
| 98 | + }); |
| 99 | + describe('with mutateRows service through insert', () => { |
| 100 | + before(async () => { |
| 101 | + service.setService({ |
| 102 | + mutateRows: emitTableNotExistsError, |
| 103 | + }); |
| 104 | + }); |
| 105 | + it('should produce human readable error when passing through gax', async () => { |
| 106 | + const timestamp = new Date(); |
| 107 | + const rowsToInsert = [ |
| 108 | + { |
| 109 | + key: 'r2', |
| 110 | + data: { |
| 111 | + cf1: { |
| 112 | + c1: { |
| 113 | + value: 'test-value2', |
| 114 | + labels: [], |
| 115 | + timestamp, |
| 116 | + }, |
| 117 | + }, |
| 118 | + }, |
| 119 | + }, |
| 120 | + ]; |
| 121 | + try { |
| 122 | + await table.insert(rowsToInsert); |
| 123 | + } catch (err) { |
| 124 | + checkTableNotExistError(err); |
| 125 | + return; |
| 126 | + } |
| 127 | + assert.fail('An error should have been thrown by the stream'); |
| 128 | + }); |
| 129 | + }); |
| 130 | + describe('with sampleRowKeys', () => { |
| 131 | + before(async () => { |
| 132 | + service.setService({ |
| 133 | + sampleRowKeys: emitTableNotExistsError, |
| 134 | + }); |
| 135 | + }); |
| 136 | + it('should produce human readable error when passing through gax', async () => { |
| 137 | + try { |
| 138 | + await table.sampleRowKeys({}); |
| 139 | + } catch (err) { |
| 140 | + checkTableNotExistError(err); |
| 141 | + return; |
| 142 | + } |
| 143 | + assert.fail('An error should have been thrown by the stream'); |
| 144 | + }); |
| 145 | + }); |
| 146 | + }); |
| 147 | + }); |
| 148 | + after(async () => { |
| 149 | + server.shutdown(() => {}); |
| 150 | + }); |
| 151 | +}); |
0 commit comments