python-fmrest icon indicating copy to clipboard operation
python-fmrest copied to clipboard

FileMakerError adds too much abstraction

Open steveWinter opened this issue 5 years ago • 1 comments

Currently if FileMaker server returns an error an exception of type FileMakerError is raised. The same exception is used for all error types.

The problem that I am finding is that I now have to parse a lengthy text string to determine what the error actually was. In many instances this wouldn't be an issues, since it's likely to be fatal to the program execution and I'd need to log the exception (for example) and then provide some generic error response.

There are however times when you want to catch a specific error and behave differently for those. Immediate use cases I'm dealing with are

  • no records (401)
  • validation failed (503 - 507)

Would it be possible to either raise alternative exceptions for these errors, or expose the error code in the current exception?

steveWinter avatar Nov 19 '19 15:11 steveWinter

Currently, everything is a FileMakerError, but you can already get the error code directly from the Server.last_error property (returns an int, no need to parse text).

Example:

fms = fmrest.Server(...)
fms.login()
try:
    fms.find(query=[{'name': 'non-existing'}])
except FileMakerError:
    print(fms.last_error) # prints 401

By the way: the last script result is also always accessible through the last_script_result property.

Having separate exceptions (at least for the common cases) might be something to look into though. It probably reads nicer than the generic FileMakerError.

Maybe something like this:

try:
  fms.find(...)
except NoMatch:
  ...

We could subclass FileMakerError and thus keep it backward-compatible for existing implementations.

davidhamann avatar Nov 19 '19 16:11 davidhamann