How throw meaningful errors from Companion

How can I throw errors from Companion so that the message appears in the client (instead of ‘internal server error’)?

I’m building a custom provider to support uploading directly from Vimeo, but Vimeo will only give you a download link if you’re on a Pro plan. I’d like to Companion to check if the account type and throw error if it’s not Pro.

I tried sending an error in the companion callback, but it always appears on the client as ‘internal server error’ which isn’t useful.

are you using the Standalone server? If you are using the standalone server, you can add a status property to the error like

err = new Error('some error message')
err.status = 400

and this status code will be relayed to the client instead.

Hey @ifedapoolarewaju, thanks for the quick reply :blush:

I’m not using the Standalone server. I need to add customProviders to the options so I just have a simple express server that mounts companion.

The first thing I tried to do is interrupt the auth process in some way, but there doesn’t seem to be any hooks from the Provider object into that process .

The next thing I tried was (also on companion) checking in the account type in the Provider’s list call and calling done(error), but that always returns a 500 error code to the client, even if I set the error.status to some other code (also the response body that gets returned is HTML).

Ok, I managed to do it on the client like this:

class Vimeo extends Plugin
  onFirstRender () {
    return this.view.provider.list()
    .then(res => {
      if (res.account == 'basic' || res.account == 'plus') {{ message: 'Needs a Vimeo pro account', details: 'Vimeo only allows us to download videos from Pro accounts or higher'}, 'error', 10000)
      } else {

(I also needed to change the list method on the companion server to return the account type)

if you are using your own custom server then this issue is related with how you are handling errors on your express server.

You can handle errors in a similar way that the companion standalone does it here. In your case, you can do something like this:

app.use((err, req, res, next) => {
    res.status(err.status || 500).json({ message: err.message })

Keep in mind that this error middleware should be added after the has been initiated.

After doing this, then you can go back to following the suggestion I made here