hgaol opened a new pull request #2390: api server exception management and code 
optimization (#397)
URL: https://github.com/apache/incubator-dolphinscheduler/pull/2390
 
 
   }
   
   ## *Tips*
   - *Thanks very much for contributing to Apache DolphinScheduler.*
   - *Please review 
https://dolphinscheduler.apache.org/en-us/community/index.html before opening a 
pull request.*
   
   ## What is the purpose of the pull request
   
   Currently, there are many `try-catch` code in `api-server`'s controller. 
This pr clean the code, use an annotation to point out which `Status` should be 
return.
   
   i.e.
   Before
   ```java
   public Result generateToken(@RequestAttribute(value = 
Constants.SESSION_USER) User loginUser,
                                 @RequestParam(value = "userId") int userId,
                                 @RequestParam(value = "expireTime") String 
expireTime){
       logger.info("login user {}, generate token , userId : {} , token expire 
time : {}",loginUser,userId,expireTime);
       try {
           Map<String, Object> result = 
accessTokenService.generateToken(userId, expireTime);
           return returnDataList(result);
       }catch (Exception e){
           logger.error(GENERATE_TOKEN_ERROR.getMsg(),e);
           return error(GENERATE_TOKEN_ERROR.getCode(), 
GENERATE_TOKEN_ERROR.getMsg());
       }
   }
   ```
   
   After
   ```java
   @ControllerException(GENERATE_TOKEN_ERROR)
   public Result generateToken(@RequestAttribute(value = 
Constants.SESSION_USER) User loginUser,
                               @RequestParam(value = "userId") int userId,
                               @RequestParam(value = "expireTime") String 
expireTime) {
       logger.info("login user {}, generate token , userId : {} , token expire 
time : {}", loginUser, userId, expireTime);
   
       Map<String, Object> result = accessTokenService.generateToken(userId, 
expireTime);
       return returnDataList(result);
   }
   ```
   
   ## Brief change log
   
     - Add `ControllerException` annotation in 
`org.apache.dolphinscheduler.api.exceptions`.
     - Add `ApiExceptionHandler` controller advice to deal with exceptions.
     - Replace all controller functions which has `try-catch` style with 
`@ControllerException` annotation.
   
   ## Verify this pull request
   
   I've debugged the code locally, and it works well. But it's difficult to 
write tests for this code, because I should find how to throw an exception. It 
will be easy if I can write some test controllers to test this PR. Pls tell me 
if I can do that, or is there any other way for testing.
   
   Thanks in advance!
   
   

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
[email protected]


With regards,
Apache Git Services

Reply via email to