Add webapp exception handler :)

This commit is contained in:
Anthony Corbacho 2016-10-28 20:19:32 +09:00
parent 21f9288ef7
commit fe380abf6f
3 changed files with 145 additions and 0 deletions

View file

@ -0,0 +1,59 @@
/*
* 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.zeppelin.rest.exception;
import static javax.ws.rs.core.Response.Status.NOT_FOUND;
import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.Response;
import org.apache.zeppelin.utils.ExceptionUtils;
/**
* Not Found handler for WebApplicationException.
*
*/
public class NotFoundException extends WebApplicationException {
private static final long serialVersionUID = 2459398393216512293L;
/**
* Create a HTTP 404 (Not Found) exception.
*/
public NotFoundException() {
super(ExceptionUtils.jsonResponse(NOT_FOUND));
}
/**
* Create a HTTP 404 (Not Found) exception.
* @param message the String that is the entity of the 404 response.
*/
public NotFoundException(String message) {
super(notFoundJson(message));
}
private static Response notFoundJson(String message) {
return ExceptionUtils.jsonResponseContent(NOT_FOUND, message);
}
public NotFoundException(Throwable cause) {
super(cause, notFoundJson(cause.getMessage()));
}
public NotFoundException(Throwable cause, String message) {
super(cause, notFoundJson(message));
}
}

View file

@ -0,0 +1,50 @@
/*
* 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.zeppelin.rest.exception;
import static javax.ws.rs.core.Response.Status.FORBIDDEN;
import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.Response;
import org.apache.zeppelin.utils.ExceptionUtils;
/**
* UnauthorizedException handler for WebApplicationException.
*
*/
public class UnauthorizedException extends WebApplicationException {
private static final long serialVersionUID = 4394749068760407567L;
private static final String UNAUTHORIZED_MSG = "Authorization required";
public UnauthorizedException() {
super(unauthorizedJson(UNAUTHORIZED_MSG));
}
private static Response unauthorizedJson(String message) {
return ExceptionUtils.jsonResponseContent(FORBIDDEN, message);
}
public UnauthorizedException(Throwable cause, String message) {
super(cause, unauthorizedJson(message));
}
public UnauthorizedException(String message) {
super(unauthorizedJson(message));
}
}

View file

@ -0,0 +1,36 @@
/*
* 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.zeppelin.utils;
import javax.ws.rs.core.Response.Status;
import org.apache.zeppelin.server.JsonResponse;
/**
* Utility method for exception in rest api.
*
*/
public class ExceptionUtils {
public static javax.ws.rs.core.Response jsonResponse(Status status) {
return new JsonResponse<>(status).build();
}
public static javax.ws.rs.core.Response jsonResponseContent(Status status, String message) {
return new JsonResponse<>(status, message).build();
}
}