break60
5 years ago
10 changed files with 305 additions and 73 deletions
@ -0,0 +1,34 @@
|
||||
/* |
||||
* Licensed to the Apache Software Foundation (ASF) under one or more |
||||
* contributor license agreements. See the NOTICE file distributed with |
||||
* this work for additional information regarding copyright ownership. |
||||
* The ASF licenses this file to You under the Apache License, Version 2.0 |
||||
* (the "License"); you may not use this file except in compliance with |
||||
* the License. You may obtain a copy of the License at |
||||
* |
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software |
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
* See the License for the specific language governing permissions and |
||||
* limitations under the License. |
||||
*/ |
||||
package org.apache.dolphinscheduler.api.exceptions; |
||||
|
||||
import org.apache.dolphinscheduler.api.enums.Status; |
||||
|
||||
import java.lang.annotation.Retention; |
||||
import java.lang.annotation.Target; |
||||
|
||||
import static java.lang.annotation.ElementType.METHOD; |
||||
import static java.lang.annotation.RetentionPolicy.RUNTIME; |
||||
|
||||
/** |
||||
* controller exception annotation |
||||
*/ |
||||
@Retention(RUNTIME) |
||||
@Target(METHOD) |
||||
public @interface ApiException { |
||||
Status value(); |
||||
} |
@ -0,0 +1,48 @@
|
||||
/* |
||||
* Licensed to the Apache Software Foundation (ASF) under one or more |
||||
* contributor license agreements. See the NOTICE file distributed with |
||||
* this work for additional information regarding copyright ownership. |
||||
* The ASF licenses this file to You under the Apache License, Version 2.0 |
||||
* (the "License"); you may not use this file except in compliance with |
||||
* the License. You may obtain a copy of the License at |
||||
* |
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software |
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
* See the License for the specific language governing permissions and |
||||
* limitations under the License. |
||||
*/ |
||||
package org.apache.dolphinscheduler.api.exceptions; |
||||
|
||||
import org.apache.dolphinscheduler.api.enums.Status; |
||||
import org.apache.dolphinscheduler.api.utils.Result; |
||||
import org.slf4j.Logger; |
||||
import org.slf4j.LoggerFactory; |
||||
import org.springframework.web.bind.annotation.ControllerAdvice; |
||||
import org.springframework.web.bind.annotation.ExceptionHandler; |
||||
import org.springframework.web.bind.annotation.ResponseBody; |
||||
import org.springframework.web.method.HandlerMethod; |
||||
|
||||
/** |
||||
* Exception Handler |
||||
*/ |
||||
@ControllerAdvice |
||||
@ResponseBody |
||||
public class ApiExceptionHandler { |
||||
|
||||
private static final Logger logger = LoggerFactory.getLogger(ApiExceptionHandler.class); |
||||
|
||||
@ExceptionHandler(Exception.class) |
||||
public Result exceptionHandler(Exception e, HandlerMethod hm) { |
||||
logger.error(e.getMessage(), e); |
||||
ApiException ce = hm.getMethodAnnotation(ApiException.class); |
||||
if (ce == null) { |
||||
return Result.errorWithArgs(Status.INTERNAL_SERVER_ERROR_ARGS, e.getMessage()); |
||||
} |
||||
Status st = ce.value(); |
||||
return Result.error(st); |
||||
} |
||||
|
||||
} |
@ -0,0 +1,42 @@
|
||||
/* |
||||
* Licensed to the Apache Software Foundation (ASF) under one or more |
||||
* contributor license agreements. See the NOTICE file distributed with |
||||
* this work for additional information regarding copyright ownership. |
||||
* The ASF licenses this file to You under the Apache License, Version 2.0 |
||||
* (the "License"); you may not use this file except in compliance with |
||||
* the License. You may obtain a copy of the License at |
||||
* |
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software |
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
* See the License for the specific language governing permissions and |
||||
* limitations under the License. |
||||
*/ |
||||
package org.apache.dolphinscheduler.api.exceptions; |
||||
|
||||
import org.apache.dolphinscheduler.api.controller.AccessTokenController; |
||||
import org.apache.dolphinscheduler.api.enums.Status; |
||||
import org.apache.dolphinscheduler.api.utils.Result; |
||||
import org.apache.dolphinscheduler.dao.entity.User; |
||||
import org.junit.Assert; |
||||
import org.junit.Test; |
||||
import org.springframework.web.method.HandlerMethod; |
||||
|
||||
import java.lang.reflect.Method; |
||||
|
||||
import static org.junit.Assert.*; |
||||
|
||||
public class ApiExceptionHandlerTest { |
||||
|
||||
@Test |
||||
public void exceptionHandler() throws NoSuchMethodException { |
||||
ApiExceptionHandler handler = new ApiExceptionHandler(); |
||||
AccessTokenController controller = new AccessTokenController(); |
||||
Method method = controller.getClass().getMethod("createToken", User.class, int.class, String.class, String.class); |
||||
HandlerMethod hm = new HandlerMethod(controller, method); |
||||
Result result = handler.exceptionHandler(new RuntimeException("test exception"), hm); |
||||
Assert.assertEquals(Status.CREATE_ACCESS_TOKEN_ERROR.getCode(),result.getCode().intValue()); |
||||
} |
||||
} |
@ -0,0 +1,48 @@
|
||||
/* |
||||
* Licensed to the Apache Software Foundation (ASF) under one or more |
||||
* contributor license agreements. See the NOTICE file distributed with |
||||
* this work for additional information regarding copyright ownership. |
||||
* The ASF licenses this file to You under the Apache License, Version 2.0 |
||||
* (the "License"); you may not use this file except in compliance with |
||||
* the License. You may obtain a copy of the License at |
||||
* |
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software |
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
* See the License for the specific language governing permissions and |
||||
* limitations under the License. |
||||
*/ |
||||
package org.apache.dolphinscheduler.api.utils; |
||||
|
||||
import org.apache.dolphinscheduler.api.enums.Status; |
||||
import org.junit.Assert; |
||||
import org.junit.Test; |
||||
|
||||
import java.util.HashMap; |
||||
|
||||
import static org.junit.Assert.*; |
||||
|
||||
public class ResultTest { |
||||
|
||||
@Test |
||||
public void success() { |
||||
HashMap<String, String> map = new HashMap<>(); |
||||
map.put("testdata", "test"); |
||||
Result ret = Result.success(map); |
||||
Assert.assertEquals(Status.SUCCESS.getCode(), ret.getCode().intValue()); |
||||
} |
||||
|
||||
@Test |
||||
public void error() { |
||||
Result ret = Result.error(Status.ACCESS_TOKEN_NOT_EXIST); |
||||
Assert.assertEquals(Status.ACCESS_TOKEN_NOT_EXIST.getCode(), ret.getCode().intValue()); |
||||
} |
||||
|
||||
@Test |
||||
public void errorWithArgs() { |
||||
Result ret = Result.errorWithArgs(Status.INTERNAL_SERVER_ERROR_ARGS, "test internal server error"); |
||||
Assert.assertEquals(Status.INTERNAL_SERVER_ERROR_ARGS.getCode(), ret.getCode().intValue()); |
||||
} |
||||
} |
Loading…
Reference in new issue