Skip to content

Commit 77e1b00

Browse files
committed
Add prototype setting for meili errors to allow instanceof operator
1 parent fbe783b commit 77e1b00

File tree

5 files changed

+68
-3
lines changed

5 files changed

+68
-3
lines changed

src/errors/meilisearch-api-error.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,17 @@ const MeiliSearchApiError = class extends Error {
99

1010
constructor(error: MeiliSearchErrorInfo, status: number) {
1111
super(error.message)
12+
13+
// Make errors comparison possible. ex: error instanceof MeiliSearchApiError.
14+
Object.setPrototypeOf(this, MeiliSearchApiError.prototype)
15+
1216
this.name = 'MeiliSearchApiError'
1317

1418
this.code = error.code
1519
this.type = error.type
1620
this.link = error.link
1721
this.message = error.message
1822
this.httpStatus = status
19-
// Make errors comparison possible. ex: error instanceof MeiliSearchApiError.
20-
Object.setPrototypeOf(this, MeiliSearchApiError.prototype)
2123

2224
if (Error.captureStackTrace) {
2325
Error.captureStackTrace(this, MeiliSearchApiError)

src/errors/meilisearch-communication-error.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ class MeiliSearchCommunicationError extends Error {
1515
) {
1616
super(message)
1717

18+
// Make errors comparison possible. ex: error instanceof MeiliSearchCommunicationError.
19+
Object.setPrototypeOf(this, MeiliSearchCommunicationError.prototype)
20+
1821
this.name = 'MeiliSearchCommunicationError'
1922

2023
if (body instanceof Response) {

src/errors/meilisearch-error.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
class MeiliSearchError extends Error {
22
constructor(message: string) {
33
super(message)
4+
5+
// Make errors comparison possible. ex: error instanceof MeiliSearchError.
6+
Object.setPrototypeOf(this, MeiliSearchError.prototype)
7+
48
this.name = 'MeiliSearchError'
59

610
if (Error.captureStackTrace) {

src/errors/meilisearch-timeout-error.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
class MeiliSearchTimeOutError extends Error {
22
constructor(message: string) {
33
super(message)
4+
5+
// Make errors comparison possible. ex: error instanceof MeiliSearchTimeOutError.
6+
Object.setPrototypeOf(this, MeiliSearchTimeOutError.prototype)
7+
48
this.name = 'MeiliSearchTimeOutError'
59

610
if (Error.captureStackTrace) {

tests/errors.test.ts

Lines changed: 53 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
import { MeiliSearch } from './utils/meilisearch-test-utils'
2-
import { MeiliSearchApiError } from '../src/errors'
2+
import {
3+
MeiliSearchError,
4+
MeiliSearchApiError,
5+
MeiliSearchCommunicationError,
6+
MeiliSearchTimeOutError,
7+
} from '../src/errors'
38
import 'jest-fetch-mock'
49
import fetchMock from 'jest-fetch-mock'
510

@@ -42,4 +47,51 @@ describe('Test on updates', () => {
4247
expect(e.name).toEqual('MeiliSearchApiError')
4348
}
4449
})
50+
51+
test('MeiliSearchApiError can be compared with the instanceof operator', async () => {
52+
fetchMock.mockReject(
53+
new MeiliSearchApiError(
54+
{
55+
message: 'Some error',
56+
code: 'some_error',
57+
type: 'random_error',
58+
link: 'a link',
59+
},
60+
404
61+
)
62+
)
63+
64+
const client = new MeiliSearch({ host: 'http://localhost:9345' })
65+
try {
66+
await client.health()
67+
} catch (e: any) {
68+
expect(e instanceof MeiliSearchApiError).toEqual(true)
69+
}
70+
})
71+
72+
test('MeiliSearchCommunicationError can be compared with the instanceof operator', async () => {
73+
fetchMock.mockReject(new Error('fake error message'))
74+
const client = new MeiliSearch({ host: 'http://localhost:9345' })
75+
try {
76+
await client.health()
77+
} catch (e: any) {
78+
expect(e instanceof MeiliSearchCommunicationError).toEqual(true)
79+
}
80+
})
81+
82+
test('MeiliSearchError can be compared with the instanceof operator', () => {
83+
try {
84+
throw new MeiliSearchError('message')
85+
} catch (e: any) {
86+
expect(e instanceof MeiliSearchError).toEqual(true)
87+
}
88+
})
89+
90+
test('MeiliSearchTimeOutError can be compared with the instanceof operator', () => {
91+
try {
92+
throw new MeiliSearchTimeOutError('message')
93+
} catch (e: any) {
94+
expect(e instanceof MeiliSearchTimeOutError).toEqual(true)
95+
}
96+
})
4597
})

0 commit comments

Comments
 (0)