Skip to content

Commit 9892d63

Browse files
Add data field in serialization errors (#1100)
* Add data field in serialization errors * Add test for data field in serialization errors Co-authored-by: Tomas Della Vedova <[email protected]>
1 parent a28f9c3 commit 9892d63

File tree

4 files changed

+14
-8
lines changed

4 files changed

+14
-8
lines changed

lib/Serializer.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ class Serializer {
1515
try {
1616
var json = JSON.stringify(object)
1717
} catch (err) {
18-
throw new SerializationError(err.message)
18+
throw new SerializationError(err.message, object)
1919
}
2020
return json
2121
}
@@ -25,7 +25,7 @@ class Serializer {
2525
try {
2626
var object = sjson.parse(json)
2727
} catch (err) {
28-
throw new DeserializationError(err.message)
28+
throw new DeserializationError(err.message, json)
2929
}
3030
return object
3131
}

lib/errors.d.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,15 @@ export declare class NoLivingConnectionsError extends ElasticsearchClientError {
3333
export declare class SerializationError extends ElasticsearchClientError {
3434
name: string;
3535
message: string;
36-
constructor(message: string);
36+
data: object;
37+
constructor(message: string, data: object);
3738
}
3839

3940
export declare class DeserializationError extends ElasticsearchClientError {
4041
name: string;
4142
message: string;
42-
constructor(message: string);
43+
data: string;
44+
constructor(message: string, data: string);
4345
}
4446

4547
export declare class ConfigurationError extends ElasticsearchClientError {

lib/errors.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,20 +42,22 @@ class NoLivingConnectionsError extends ElasticsearchClientError {
4242
}
4343

4444
class SerializationError extends ElasticsearchClientError {
45-
constructor (message) {
46-
super(message)
45+
constructor (message, data) {
46+
super(message, data)
4747
Error.captureStackTrace(this, SerializationError)
4848
this.name = 'SerializationError'
4949
this.message = message || 'Serialization Error'
50+
this.data = data
5051
}
5152
}
5253

5354
class DeserializationError extends ElasticsearchClientError {
54-
constructor (message) {
55-
super(message)
55+
constructor (message, data) {
56+
super(message, data)
5657
Error.captureStackTrace(this, DeserializationError)
5758
this.name = 'DeserializationError'
5859
this.message = message || 'Deserialization Error'
60+
this.data = data
5961
}
6062
}
6163

test/unit/errors.test.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ test('SerializationError', t => {
4444
t.true(err instanceof Error)
4545
t.true(err instanceof errors.ElasticsearchClientError)
4646
t.false(err.hasOwnProperty('meta'))
47+
t.true(err.hasOwnProperty('data'))
4748
t.end()
4849
})
4950

@@ -52,6 +53,7 @@ test('DeserializationError', t => {
5253
t.true(err instanceof Error)
5354
t.true(err instanceof errors.ElasticsearchClientError)
5455
t.false(err.hasOwnProperty('meta'))
56+
t.true(err.hasOwnProperty('data'))
5557
t.end()
5658
})
5759

0 commit comments

Comments
 (0)