Skip to content

Commit 33331e4

Browse files
committed
Add error handler to handle missing host
1 parent e8d3f50 commit 33331e4

File tree

3 files changed

+31
-3
lines changed

3 files changed

+31
-3
lines changed

controllers/meilisearch.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@ async function sendCtx (ctx, fct) {
2929

3030
async function getHookedCollections () {
3131
const store = await meilisearch.store()
32-
return await store.getStoreKey('meilisearch_hooked')
32+
const hookedCollections = await store.getStoreKey('meilisearch_hooked')
33+
return hookedCollections || []
3334
}
3435

3536
async function getCredentials () {

services/client.js

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,16 @@
11
'use strict'
2-
32
const { MeiliSearch } = require('meilisearch')
43

5-
module.exports = (config) => new MeiliSearch(config)
4+
const MeiliSearchError = () => strapi.plugins.meilisearch.services.error
5+
6+
module.exports = (config) => {
7+
try {
8+
return new MeiliSearch(config)
9+
} catch (e) {
10+
throw new (MeiliSearchError())({
11+
message: 'Please provide a valid host for your MeiliSearch instance',
12+
link:
13+
'https://docs.meilisearch.com/learn/getting_started/installation.html#download-and-launch'
14+
})
15+
}
16+
}

services/error.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
class MeiliSearchError extends Error {
2+
constructor ({ message = 'Something went wrong with MeiliSearch', title = 'Operation on MeiliSearch failed', link }, ...params) {
3+
super(...params)
4+
5+
if (Error.captureStackTrace) {
6+
Error.captureStackTrace(this, MeiliSearchError)
7+
}
8+
this.name = 'MeiliSearchError'
9+
this.type = 'MeiliSearchError'
10+
this.message = message
11+
this.title = title
12+
this.link = link
13+
}
14+
}
15+
16+
module.exports = MeiliSearchError

0 commit comments

Comments
 (0)