Skip to content

Commit e516d31

Browse files
authored
Add error types to declaration file (#210)
* Add error types to declaration file * Update CHANGELOG.md * Change linting as per review comments
1 parent 3cd0526 commit e516d31

File tree

6 files changed

+124
-13
lines changed

6 files changed

+124
-13
lines changed

.semaphore/semaphore.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,9 +107,9 @@ blocks:
107107
- docker compose up -d && sleep 30
108108
- export NODE_OPTIONS='--max-old-space-size=1536'
109109
- npx jest --no-colors --ci test/promisified/
110-
- name: "ESLint"
110+
- name: "Lint"
111111
commands:
112-
- npx eslint lib/kafkajs
112+
- make lint
113113
- name: "Docs"
114114
commands:
115115
- make docs

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ v1.0.0 is a feature release. It is supported for all usage.
77
1. Add support for an Admin API to fetch topic offsets (#156).
88
2. Add support for Node v23 pre-built binaries (#158).
99
3. Add KafkaJS-compatible errors to promisified Admin API (createTopics, deleteGroups, deleteTopicRecords) (#159).
10+
4. Include error types within Type definitions for promisified API (#210).
1011

1112
## Fixes
1213

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ cpplint:
3838
@$(PYTHON) $(CPPLINT) --filter=$(CPPLINT_FILTER) $(CPPLINT_FILES)
3939

4040
eslint: node_modules/.dirstamp
41-
@./node_modules/.bin/eslint .
41+
@./node_modules/.bin/eslint lib
4242

4343
lib: node_modules/.dirstamp $(CONFIG_OUTPUTS)
4444
@PYTHONHTTPSVERIFY=0 $(NODE-GYP) build $(GYPBUILDARGS)

lib/kafkajs/_error.js

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -181,13 +181,12 @@ class KafkaJSTimeout extends KafkaJSError {
181181
this.name = 'KafkaJSTimeout';
182182
}
183183
}
184-
class KafkaJSLockTimeout extends KafkaJSTimeout {
185-
constructor() {
186-
super(...arguments);
187-
this.name = 'KafkaJSLockTimeout';
188-
}
189-
}
190184

185+
/**
186+
* KafkaJSCreateTopicError represents an error raised by the createTopics method for one topic.
187+
* @extends KafkaJS.KafkaJSError
188+
* @memberof KafkaJS
189+
*/
191190
class KafkaJSCreateTopicError extends KafkaJSProtocolError {
192191
constructor(e, topicName, properties) {
193192
super(e, properties);
@@ -196,6 +195,11 @@ class KafkaJSCreateTopicError extends KafkaJSProtocolError {
196195
}
197196
}
198197

198+
/**
199+
* KafkaJSDeleteGroupsError represents an error raised by the deleteGroups method.
200+
* @extends KafkaJS.KafkaJSError
201+
* @memberof KafkaJS
202+
*/
199203
class KafkaJSDeleteGroupsError extends KafkaJSError {
200204
constructor(e, groups) {
201205
super(e);
@@ -204,6 +208,11 @@ class KafkaJSDeleteGroupsError extends KafkaJSError {
204208
}
205209
}
206210

211+
/**
212+
* KafkaJSDeleteTopicRecordsError represents an error raised by the deleteTopicRecords method.
213+
* @extends KafkaJS.KafkaJSError
214+
* @memberof KafkaJS
215+
*/
207216
class KafkaJSDeleteTopicRecordsError extends KafkaJSError {
208217
constructor({ partitions }) {
209218
/*
@@ -278,7 +287,6 @@ module.exports = {
278287
KafkaJSGroupCoordinatorNotFound,
279288
KafkaJSNotImplemented,
280289
KafkaJSTimeout,
281-
KafkaJSLockTimeout,
282290
KafkaJSCreateTopicError,
283291
KafkaJSDeleteGroupsError,
284292
KafkaJSDeleteTopicRecordsError,

src/admin.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -998,8 +998,8 @@ NAN_METHOD(AdminClient::NodeConnect) {
998998
// on the background thread.
999999
// We will deactivate them if the connection fails.
10001000
// Because the Admin Client connect is synchronous, we can do this within
1001-
// AdminClient::Connect as well, but we do it here to keep the code similiar to
1002-
// the Producer and Consumer.
1001+
// AdminClient::Connect as well, but we do it here to keep the code similiar
1002+
// to the Producer and Consumer.
10031003
client->ActivateDispatchers();
10041004

10051005
Baton b = client->Connect();

types/kafkajs.d.ts

Lines changed: 103 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@ import {
1212
IsolationLevel
1313
} from './rdkafka'
1414

15+
import {
16+
CODES
17+
} from './errors';
18+
1519
// Admin API related interfaces, types etc; and Error types are common, so
1620
// just re-export them from here too.
1721
export {
@@ -24,7 +28,7 @@ export {
2428
Node,
2529
AclOperationTypes,
2630
Uuid,
27-
IsolationLevel
31+
IsolationLevel,
2832
} from './rdkafka'
2933

3034
export interface OauthbearerProviderResponse {
@@ -427,3 +431,101 @@ export type Admin = {
427431
isolationLevel: IsolationLevel
428432
}): Promise<Array<SeekEntry & { high: string; low: string }>>
429433
}
434+
435+
436+
export function isKafkaJSError(error: Error): boolean;
437+
438+
export const ErrorCodes: typeof CODES.ERRORS;
439+
440+
export class KafkaJSError extends Error {
441+
readonly message: Error['message']
442+
readonly name: string
443+
readonly retriable: boolean
444+
readonly fatal: boolean
445+
readonly abortable: boolean
446+
readonly code: number
447+
constructor(e: Error | string, metadata?: KafkaJSErrorMetadata)
448+
}
449+
450+
export class KafkaJSProtocolError extends KafkaJSError {
451+
constructor(e: Error | string)
452+
}
453+
454+
export class KafkaJSCreateTopicError extends KafkaJSError {
455+
readonly topic: string
456+
constructor(e: Error | string, topicName: string, metadata?: KafkaJSErrorMetadata)
457+
}
458+
459+
export class KafkaJSDeleteGroupsError extends KafkaJSError {
460+
readonly groups: DeleteGroupsResult[]
461+
constructor(e: Error | string, groups?: KafkaJSDeleteGroupsErrorGroups[])
462+
}
463+
464+
export class KafkaJSDeleteTopicRecordsError extends KafkaJSError {
465+
readonly partitions: KafkaJSDeleteTopicRecordsErrorPartition[]
466+
constructor(metadata: KafkaJSDeleteTopicRecordsErrorTopic)
467+
}
468+
469+
export interface KafkaJSDeleteGroupsErrorGroups {
470+
groupId: string
471+
errorCode: number
472+
error: KafkaJSError
473+
}
474+
475+
export interface KafkaJSDeleteTopicRecordsErrorTopic {
476+
topic: string
477+
partitions: KafkaJSDeleteTopicRecordsErrorPartition[]
478+
}
479+
480+
export interface KafkaJSDeleteTopicRecordsErrorPartition {
481+
partition: number
482+
offset: string
483+
error: KafkaJSError
484+
}
485+
486+
export class KafkaJSAggregateError extends Error {
487+
readonly errors: (Error | string)[]
488+
constructor(message: Error | string, errors: (Error | string)[])
489+
}
490+
491+
export class KafkaJSOffsetOutOfRange extends KafkaJSProtocolError {
492+
readonly topic: string
493+
readonly partition: number
494+
constructor(e: Error | string, metadata?: KafkaJSErrorMetadata)
495+
}
496+
497+
export class KafkaJSConnectionError extends KafkaJSError {
498+
constructor(e: Error | string, metadata?: KafkaJSErrorMetadata)
499+
}
500+
501+
export class KafkaJSRequestTimeoutError extends KafkaJSError {
502+
constructor(e: Error | string, metadata?: KafkaJSErrorMetadata)
503+
}
504+
505+
export class KafkaJSPartialMessageError extends KafkaJSError {
506+
constructor()
507+
}
508+
509+
export class KafkaJSSASLAuthenticationError extends KafkaJSError {
510+
constructor()
511+
}
512+
513+
export class KafkaJSGroupCoordinatorNotFound extends KafkaJSError {
514+
constructor()
515+
}
516+
517+
export class KafkaJSNotImplemented extends KafkaJSError {
518+
constructor()
519+
}
520+
521+
export class KafkaJSTimeout extends KafkaJSError {
522+
constructor()
523+
}
524+
525+
export interface KafkaJSErrorMetadata {
526+
retriable?: boolean
527+
fatal?: boolean
528+
abortable?: boolean
529+
stack?: string
530+
code?: number
531+
}

0 commit comments

Comments
 (0)