-
Notifications
You must be signed in to change notification settings - Fork 161
Commit
Ensure the backend returns HTTP 403 (Forbidden) instead of HTTP 500 (Internal Server Error) when any entity exceeds the maximum allowed limit as defined by service settings.
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
/******************************************************************************* | ||
* Copyright (c) 2024 Eurotech and/or its affiliates and others | ||
* | ||
* This program and the accompanying materials are made | ||
* available under the terms of the Eclipse Public License 2.0 | ||
* which is available at https://www.eclipse.org/legal/epl-2.0/ | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 | ||
* | ||
* Contributors: | ||
* Eurotech - initial API and implementation | ||
*******************************************************************************/ | ||
package org.eclipse.kapua.commons.rest.errors; | ||
|
||
import javax.inject.Inject; | ||
import javax.ws.rs.core.Response; | ||
import javax.ws.rs.core.Response.Status; | ||
import javax.ws.rs.ext.ExceptionMapper; | ||
import javax.ws.rs.ext.Provider; | ||
|
||
import org.eclipse.kapua.KapuaMaxNumberOfItemsReachedException; | ||
import org.eclipse.kapua.commons.rest.model.errors.MaxNumberOfItemsReachedExceptionInfo; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
@Provider | ||
public class KapuaMaxNumberOfItemsReachedExceptionMapper implements ExceptionMapper<KapuaMaxNumberOfItemsReachedException> { | ||
|
||
private static final Logger LOG = LoggerFactory.getLogger(KapuaMaxNumberOfItemsReachedExceptionMapper.class); | ||
Check warning on line 29 in commons-rest/errors/src/main/java/org/eclipse/kapua/commons/rest/errors/KapuaMaxNumberOfItemsReachedExceptionMapper.java Codecov / codecov/patchcommons-rest/errors/src/main/java/org/eclipse/kapua/commons/rest/errors/KapuaMaxNumberOfItemsReachedExceptionMapper.java#L29
|
||
private final boolean showStackTrace; | ||
|
||
|
||
@Inject | ||
public KapuaMaxNumberOfItemsReachedExceptionMapper(ExceptionConfigurationProvider exceptionConfigurationProvider) { | ||
this.showStackTrace = exceptionConfigurationProvider.showStackTrace(); | ||
} | ||
Check warning on line 36 in commons-rest/errors/src/main/java/org/eclipse/kapua/commons/rest/errors/KapuaMaxNumberOfItemsReachedExceptionMapper.java Codecov / codecov/patchcommons-rest/errors/src/main/java/org/eclipse/kapua/commons/rest/errors/KapuaMaxNumberOfItemsReachedExceptionMapper.java#L34-L36
|
||
|
||
|
||
@Override | ||
public Response toResponse(KapuaMaxNumberOfItemsReachedException kapuaMaxNumberOfItemsReachedException) { | ||
LOG.error(kapuaMaxNumberOfItemsReachedException.getMessage(), kapuaMaxNumberOfItemsReachedException); | ||
return Response | ||
.status(Status.FORBIDDEN) | ||
.entity(new MaxNumberOfItemsReachedExceptionInfo(Status.FORBIDDEN.getStatusCode(), kapuaMaxNumberOfItemsReachedException, showStackTrace)) | ||
.build(); | ||
Check warning on line 45 in commons-rest/errors/src/main/java/org/eclipse/kapua/commons/rest/errors/KapuaMaxNumberOfItemsReachedExceptionMapper.java Codecov / codecov/patchcommons-rest/errors/src/main/java/org/eclipse/kapua/commons/rest/errors/KapuaMaxNumberOfItemsReachedExceptionMapper.java#L41-L45
|
||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
/******************************************************************************* | ||
* Copyright (c) 2024 Eurotech and/or its affiliates and others | ||
* | ||
* This program and the accompanying materials are made | ||
* available under the terms of the Eclipse Public License 2.0 | ||
* which is available at https://www.eclipse.org/legal/epl-2.0/ | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 | ||
* | ||
* Contributors: | ||
* Eurotech - initial API and implementation | ||
*******************************************************************************/ | ||
package org.eclipse.kapua.commons.rest.model.errors; | ||
|
||
import javax.xml.bind.annotation.XmlAccessType; | ||
import javax.xml.bind.annotation.XmlAccessorType; | ||
import javax.xml.bind.annotation.XmlElement; | ||
import javax.xml.bind.annotation.XmlRootElement; | ||
|
||
import org.eclipse.kapua.KapuaMaxNumberOfItemsReachedException; | ||
|
||
@XmlRootElement(name = "maxNumberOfItemsReachedExceptionInfo") | ||
@XmlAccessorType(XmlAccessType.FIELD) | ||
public class MaxNumberOfItemsReachedExceptionInfo extends ExceptionInfo { | ||
|
||
@XmlElement(name = "entityType") | ||
private String entityType; | ||
|
||
|
||
/** | ||
* Constructor. | ||
* | ||
* @since 2.0.0 | ||
*/ | ||
protected MaxNumberOfItemsReachedExceptionInfo() { | ||
super(); | ||
} | ||
Check warning on line 37 in commons-rest/model/src/main/java/org/eclipse/kapua/commons/rest/model/errors/MaxNumberOfItemsReachedExceptionInfo.java Codecov / codecov/patchcommons-rest/model/src/main/java/org/eclipse/kapua/commons/rest/model/errors/MaxNumberOfItemsReachedExceptionInfo.java#L36-L37
|
||
|
||
|
||
/** | ||
* Constructor. | ||
* | ||
* @param httpStatusCode The http status code of the response containing this info | ||
* @param kapuaMaxNumberOfItemsReachedException The root exception. | ||
* @since 2.0.0 | ||
*/ | ||
public MaxNumberOfItemsReachedExceptionInfo(int httpStatusCode, KapuaMaxNumberOfItemsReachedException kapuaMaxNumberOfItemsReachedException, boolean showStackTrace) { | ||
super(httpStatusCode, kapuaMaxNumberOfItemsReachedException, showStackTrace); | ||
this.entityType = kapuaMaxNumberOfItemsReachedException.getEntityType(); | ||
} | ||
Check warning on line 50 in commons-rest/model/src/main/java/org/eclipse/kapua/commons/rest/model/errors/MaxNumberOfItemsReachedExceptionInfo.java Codecov / codecov/patchcommons-rest/model/src/main/java/org/eclipse/kapua/commons/rest/model/errors/MaxNumberOfItemsReachedExceptionInfo.java#L48-L50
|
||
|
||
|
||
/** | ||
* Gets the {@link KapuaMaxNumberOfItemsReachedException#getEntityType()}. | ||
* | ||
* @return The {@link KapuaMaxNumberOfItemsReachedException#getEntityType()}. | ||
* @since 2.0.0 | ||
*/ | ||
public String getEntityType() { | ||
return entityType; | ||
Check warning on line 60 in commons-rest/model/src/main/java/org/eclipse/kapua/commons/rest/model/errors/MaxNumberOfItemsReachedExceptionInfo.java Codecov / codecov/patchcommons-rest/model/src/main/java/org/eclipse/kapua/commons/rest/model/errors/MaxNumberOfItemsReachedExceptionInfo.java#L60
|
||
} | ||
|
||
} |