Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(instance): auto start error 404 instead of 500 #335

Merged
merged 4 commits into from
Feb 2, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion packages/channels/src/base/api.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Request, Router, Response, RequestHandler, NextFunction } from 'express'
import { AutoStartNotFoundError } from '..'
import { Logger } from './logger'
import { ChannelService } from './service'

Expand Down Expand Up @@ -41,7 +42,17 @@ export class ChannelApiManager {
this.asyncMiddleware(async (req, res, next) => {
const nreq = req as ChannelApiRequest
nreq.scope = req.params.scope
await this.service.require(nreq.scope)

try {
await this.service.require(nreq.scope)
} catch (e) {
if (e instanceof AutoStartNotFoundError) {
return res.sendStatus(404)
} else {
throw e
}
}

await fn(nreq, res, next)
})
)
Expand Down
7 changes: 7 additions & 0 deletions packages/channels/src/base/channel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,13 @@ export interface ChannelStateManager {
del(scope: string): void
}

export class AutoStartNotFoundError extends Error {
constructor(msg: string) {
super(msg)
Object.setPrototypeOf(this, AutoStartNotFoundError.prototype)
}
}

export abstract class ChannelTemplate<
TConfig extends ChannelConfig,
TService extends ChannelService<TConfig, any>,
Expand Down
15 changes: 12 additions & 3 deletions packages/server/src/instances/lifetime/service.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { uuid } from '@botpress/messaging-base'
import { Channel } from '@botpress/messaging-channels'
import { AutoStartNotFoundError, Channel } from '@botpress/messaging-channels'
import { DispatchService, Logger, Service, DistributedService } from '@botpress/messaging-engine'
import { ChannelService } from '../../channels/service'
import { ConduitService } from '../../conduits/service'
Expand Down Expand Up @@ -129,8 +129,17 @@ export class InstanceLifetimeService extends Service {
}

private async handleAutoStart(channel: Channel, providerName: string) {
const provider = await this.providers.getByName(providerName)
const conduit = await this.conduits.getByProviderAndChannel(provider.id, channel.meta.id)
const provider = await this.providers.fetchByName(providerName)
if (!provider) {
throw new AutoStartNotFoundError(`Failed to auto start because provider ${providerName} does not exist`)
}

const conduit = await this.conduits.fetchByProviderAndChannel(provider.id, channel.meta.id)
if (!conduit) {
throw new AutoStartNotFoundError(
`Failed to auto start because conduit ${provider.id}:${channel.meta.id} does not exist`
)
}

const status = await this.status.fetch(conduit.id)
if ((status?.numberOfErrors || 0) >= MAX_ALLOWED_FAILURES) {
Expand Down