How throw meaningful errors from Companion

Started by yonahforst4 replies1,830 views
yonahforstOriginal post
#1

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.

ifedapoolarewaju
#2

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.

yonahforstReplying to #2
#3

Hey @ifedapoolarewaju, thanks for the quick reply 😊

I’m not using the Standalone server. I need to add customProviders to the companion.app 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).

yonahforst
#4

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') {
        this.view.plugin.uppy.info({ message: 'Needs a Vimeo pro account', details: 'Vimeo only allows us to download videos from Pro accounts or higher'}, 'error', 10000)
        this.view.logout()
      } else {
        this.view.getFolder()
      }
    })
  }

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

ifedapoolarewaju
#5

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) => {
    console.error(err)
    res.status(err.status || 500).json({ message: err.message })
})

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

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