#๐Ÿ”’ Returning a custom error.

18 messages ยท Page 1 of 1 (latest)

stark geyser
#
    async def add_profile(self) -> Profile | What goes here? :

        try:
            await self.connection.commit()
        except aiosqlite.IntegrityError as error:
            return error

        async with rows as cursor:
            result = await cursor.fetchone()
            if result is not None:
                return Profile(*result)
            else:
                # Return a custom error 
``` I've omitted some code to avoid some bloat, but I want to be able to return an error if I get a `aiosqlite.IntegrityError` and return an error if `if result is not None:` Where can I read about how I would do this?
keen gladeBOT
#

@stark geyser

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

lapis jolt
#

I think Exception can work?

#

Profile | Exception

pallid sky
#

why would you return an error?

pallid sky
stark geyser
pallid sky
#

it should be raised, and caught outside the function

#

in which case sure, you could print something custom

#

but don't return the error just to check if there is an error through non-idiomatic means

#

!e

class MyCustomError(Exception):
    pass

def f():
    raise MyCustomError

try:
    f()
except MyCustomError:
    print("womp-womp, something went wrong")
keen gladeBOT
#

@pallid sky :white_check_mark: Your 3.12 eval job has completed with return code 0.

womp-womp, something went wrong
stark geyser
#

I see.

#

That makes a lot more sense than what I was gonna do ๐Ÿ˜†

#

Thank you

#

!close

keen gladeBOT
#
Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.