https://linen.dev logo
How to handle not connected account error ?
# 🖥│support-forum
m
I've an example where i have fetch email tool and use it with Langchain, but when account is not connected, I got error message as a content with **"Error: No connected accounts found\n Please fix your mistakes." **which is not reliable to use, since string might change. my question how can i get a code error for example or more proper way to handle such error ?
Copy code
tsx
import { Composio } from '@composio/core'
import { LangchainProvider } from '@composio/langchain'
import { createReactAgent } from '@langchain/langgraph/prebuilt'
import { ChatOpenAI } from '@langchain/openai'

const demoTools = ['GMAIL_FETCH_EMAILS']
const composio = new Composio({
  apiKey: process.env.COMPOSIO_API_KEY,
  provider: new LangchainProvider(),
})
async function testComposioFailedTools() {
  const tools = await composio.tools.get('NotExistAccountId', {
    tools: demoTools,
  })
  console.log(
    '-- tools --',
    tools.map((tool) => ({ name: tool.name, description: tool.description })),
  )
  const agent = createReactAgent({
    tools,
    llm: new ChatOpenAI({
      model: 'gpt-4o-mini',
      temperature: 0,
      apiKey: process.env.OPENAI_API_KEY,
    }),
  })

  const result = await agent.invoke({
    messages: [
      { role: 'user', content: 'Hello, can you list my recent emails?' },
    ],
  })
  console.log('-- result --', result)
}

testComposioFailedTools()
a
Hey there, you can use 1803 err code to show No connected account.
m
There’s no error code returns from the above example, only a string !
a
Hey! Quick correction: we don’t return the error code in the response when using the TS SDK. I’ve raised this internally to handle it better. A temporary workaround would be to use the “No connected accounts found” message (although not ideal), which should be a viable option rn.
e
You shouldn’t rely on the error message text. Wrap
composio.tools.get()
in a
try/catch
and handle structured errors instead:
Copy code
js
try {
  const tools = await composio.tools.get('NotExistAccountId', { tools: ['GMAIL_FETCH_EMAILS'] })
  if (!tools.length) throw new Error('No connected accounts')
} catch (err) {
  if (err.code === 'NO_CONNECTED_ACCOUNTS' || err.status === 404) {
    console.error('No connected Composio accounts. Prompt user to connect first.')
  } else {
    console.error('Unexpected error:', err)
  }
}
You can also check with:
Copy code
js
const accounts = await composio.accounts.list()
if (!accounts.length) console.warn('No connected accounts')
This is safer and won’t break if the message text changes. I hope to help
m
Thanks @ebro_880 I think we might have an exception here, if we've multiple tools, and on of them is not connected at that case
tools.length
will be > 1 also, since I am using Langchain, I've used the tryException tool pipe and seems working for now based on text message with a mapper for the text by an internal code
Copy code
ts
export const composioErrorMapper: Record<string, string> = {
  'No connected accounts found': 'NO_CONNECTED_ACCOUNTS_FOUND',
}
a
Hey, we’ve added throwing error codes along with messages to our pipeline. The team will work on it when they have good bandwidth.
e
Yeah, if you have multiple tools, a single bad connection won’t necessarily drop
tools.length
to 0, so checking that alone isn’t sufficient. using the LangChain
tryException
pipe and mapping known error messages to internal codes is a solid workaround for now. Ideally, Composio should expose structured error codes for each tool so mixed states can be handled cleanly. until that’s available, your mapping layer is the right approach.