Is a good template causing an exception in python decorator?

Context . I have Flask routes defined for different API endpoints, and each endpoint calls a controller class with specific parameters (uid, project_id, etc.).

@app.route('/sample/route', methods=['POST']) @require_json_payload @require_fields({ 'pid', 'params' }) def route_handler(arg1, arg2): #input filtering ... try: proj_cntr.sample_method( pid = pid, ... = ... ) except ProjCntrException: #handle error #response generation ... 

The controller (proj_cntr) is responsible for determining, say, if a given PID is valid, if this user has the right to perform an action and another check of business logic.

I noticed that I c / paste a lot of code like this in different controllers:

 if not project_object: sys_logger.info('...') raise ProjCntrException('PID %d does not exist' % pid) 

Putting these checks in the decorators seems to be best done. But I'm not sure which error handling pattern is best practice if validation fails.

1) Should I create custom user exceptions (InvalidProjException, PermissionsException, etc.) for each decorator to raise?

Problems . The caller method lock will look bloated. Also, is it good to assume that the caller knows what exceptions decorators of the caller adorn?

2) The decorator passes an additional error argument to the method, and the method decides which exception to raise. Thus, the caller method knows what type of exception to expect and handle.

Problems : The approach seems a bit overworked and confusing.

Sorry for the detailed question. Any thoughts / ideas are welcome.

+7
python flask exception error-handling
source share
2 answers

I ended up using decorators and throwing certain exceptions into them. For example:

The @validate_pid raises InvalidPidException() decorator, which falls into the exception block of any consumer that calls the decorated method.

The benefits so far:

  • Controllers are much cleaner and code replication is much less.
  • A fairly universal solution, as I use these "business logic decorators" throughout my code.

Disadvantages:

  • The decorator relies on certain parameters of the key name that must be passed when the decorated method is called, but some of these parameters are not used in the method itself. This can lead to some odd “hanging” parameters in the method signature.
  • I have some minor performance issues. For example: a project validation decoder initializes a session and loads an object (Project) only for reloading in the most decorated method. It just seems a little strange.
+3
source share

Option 1 It seems logical to create a specific Exception for your decorator, because it will help the user know what exception has been thrown, if any, and why. The type of exception should, of course, appear in the docstring of the decorator so that the caller can know what to expect. So you are not making the assumption that the user knows which exception has been raised, but that he has read the document, which is more of an assumption. However, if the method is called frequently, the caller must process it every time. This part of the deal is when using a library, API, or other code.

Option 2 It does not belong to the user to determine which exception will be thrown. For example, the user should know about this through the documentation. This is a dirty and opposing intuitive API.

Reflecting on your question, I thought about how Java works, especially if you use the IDE when you call the method that you will alert - using the IDE and using the input mechanism - that you should try/catch this type of exception.

But in your case, it would be impossible to create the correct HTTPException with a specific message?

0
source share

All Articles