|
6 | 6 | import { test } from 'tap' |
7 | 7 | import * as arrow from 'apache-arrow' |
8 | 8 | import { connection } from '../../utils' |
9 | | -import { Client } from '../../../' |
| 9 | +import { Client, errors } from '../../../' |
10 | 10 |
|
11 | 11 | test('ES|QL helper', t => { |
12 | 12 | test('toRecords', t => { |
@@ -312,6 +312,77 @@ test('ES|QL helper', t => { |
312 | 312 | t.end() |
313 | 313 | }) |
314 | 314 |
|
| 315 | + t.test('Throws ResponseError when server returns a JSON error', async t => { |
| 316 | + const errorBody = { |
| 317 | + error: { |
| 318 | + root_cause: [{ |
| 319 | + type: 'illegal_argument_exception', |
| 320 | + reason: 'ES|QL type [date_range] is not supported by the Arrow format' |
| 321 | + }], |
| 322 | + type: 'illegal_argument_exception', |
| 323 | + reason: 'ES|QL type [date_range] is not supported by the Arrow format' |
| 324 | + }, |
| 325 | + status: 400 |
| 326 | + } |
| 327 | + |
| 328 | + const MockConnection = connection.buildMockConnection({ |
| 329 | + onRequest (_params) { |
| 330 | + return { |
| 331 | + body: JSON.stringify(errorBody), |
| 332 | + statusCode: 400, |
| 333 | + headers: { |
| 334 | + 'content-type': 'application/vnd.elasticsearch+json;compatible-with=9' |
| 335 | + } |
| 336 | + } |
| 337 | + } |
| 338 | + }) |
| 339 | + |
| 340 | + const client = new Client({ |
| 341 | + node: 'http://localhost:9200', |
| 342 | + Connection: MockConnection |
| 343 | + }) |
| 344 | + |
| 345 | + try { |
| 346 | + await client.helpers.esql({ query: 'FROM test-range | LIMIT 1' }).toArrowReader() |
| 347 | + t.fail('Should throw') |
| 348 | + } catch (err: any) { |
| 349 | + t.ok(err instanceof errors.ResponseError) |
| 350 | + t.equal(err.statusCode, 400) |
| 351 | + t.equal(err.message, 'illegal_argument_exception\n\tRoot causes:\n\t\tillegal_argument_exception: ES|QL type [date_range] is not supported by the Arrow format') |
| 352 | + } |
| 353 | + |
| 354 | + t.end() |
| 355 | + }) |
| 356 | + |
| 357 | + t.test('Throws ResponseError with non-JSON error body', async t => { |
| 358 | + const MockConnection = connection.buildMockConnection({ |
| 359 | + onRequest (_params) { |
| 360 | + return { |
| 361 | + body: 'internal server error', |
| 362 | + statusCode: 500, |
| 363 | + headers: { |
| 364 | + 'content-type': 'text/plain' |
| 365 | + } |
| 366 | + } |
| 367 | + } |
| 368 | + }) |
| 369 | + |
| 370 | + const client = new Client({ |
| 371 | + node: 'http://localhost:9200', |
| 372 | + Connection: MockConnection |
| 373 | + }) |
| 374 | + |
| 375 | + try { |
| 376 | + await client.helpers.esql({ query: 'FROM test | LIMIT 1' }).toArrowReader() |
| 377 | + t.fail('Should throw') |
| 378 | + } catch (err: any) { |
| 379 | + t.ok(err instanceof errors.ResponseError) |
| 380 | + t.equal(err.statusCode, 500) |
| 381 | + } |
| 382 | + |
| 383 | + t.end() |
| 384 | + }) |
| 385 | + |
315 | 386 | t.end() |
316 | 387 | }) |
317 | 388 |
|
|
0 commit comments