Skip to content

Commit

Permalink
Merge pull request #11 from catenax-ng/feat/DCMFOSS-76
Browse files Browse the repository at this point in the history
Feat/dcmfoss_76_Elegant_exception_handling
  • Loading branch information
carslen authored Sep 14, 2023
2 parents a64e90d + fa8e086 commit 7cb99e9
Show file tree
Hide file tree
Showing 18 changed files with 379 additions and 89 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
import eclipse.tractusx.demand_capacity_mgmt_specification.model.CompanyDto;
import java.util.List;
import lombok.AllArgsConstructor;
import org.eclipse.tractusx.demandcapacitymgmt.demandcapacitymgmtbackend.entities.CompanyEntity;
import org.eclipse.tractusx.demandcapacitymgmt.demandcapacitymgmtbackend.services.CompanyService;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
import eclipse.tractusx.demand_capacity_mgmt_specification.model.UnitMeasure;
import java.util.List;
import lombok.AllArgsConstructor;
import org.eclipse.tractusx.demandcapacitymgmt.demandcapacitymgmtbackend.entities.UnitMeasureEntity;
import org.eclipse.tractusx.demandcapacitymgmt.demandcapacitymgmtbackend.services.UnityOfMeasureService;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
* ******************************************************************************
* Copyright (c) 2023 BMW AG
* Copyright (c) 2023 Contributors to the Eclipse Foundation
*
* See the NOTICE file(s) distributed with this work for additional
* information regarding copyright ownership.
*
* This program and the accompanying materials are made available under the
* terms of the Apache License, Version 2.0 which is available at
* https://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.
*
* SPDX-License-Identifier: Apache-2.0
* *******************************************************************************
*/

package org.eclipse.tractusx.demandcapacitymgmt.demandcapacitymgmtbackend.exceptions.base;

import java.util.List;

public interface CustomException<T extends Exception> {
int getCode();
String getMessage();
List<String> getDetails();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
* ******************************************************************************
* Copyright (c) 2023 BMW AG
* Copyright (c) 2023 Contributors to the Eclipse Foundation
*
* See the NOTICE file(s) distributed with this work for additional
* information regarding copyright ownership.
*
* This program and the accompanying materials are made available under the
* terms of the Apache License, Version 2.0 which is available at
* https://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.
*
* SPDX-License-Identifier: Apache-2.0
* *******************************************************************************
*/

package org.eclipse.tractusx.demandcapacitymgmt.demandcapacitymgmtbackend.exceptions.base;

import java.util.List;

public interface ExceptionResponse {
int getCode();
String getMessage();
List<String> getDetails();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
* ******************************************************************************
* Copyright (c) 2023 BMW AG
* Copyright (c) 2023 Contributors to the Eclipse Foundation
*
* See the NOTICE file(s) distributed with this work for additional
* information regarding copyright ownership.
*
* This program and the accompanying materials are made available under the
* terms of the Apache License, Version 2.0 which is available at
* https://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.
*
* SPDX-License-Identifier: Apache-2.0
* *******************************************************************************
*/

package org.eclipse.tractusx.demandcapacitymgmt.demandcapacitymgmtbackend.exceptions.base;

import java.util.List;

public class ExceptionResponseImpl<T extends CustomException<?>> implements ExceptionResponse {

private final int code;
private final String message;
private final List<String> details;

public ExceptionResponseImpl(T ex) {
this.code = ex.getCode();
this.message = ex.getMessage();
this.details = ex.getDetails();
}

@Override
public int getCode() {
return code;
}

@Override
public String getMessage() {
return message;
}

@Override
public List<String> getDetails() {
return details;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,13 @@

package org.eclipse.tractusx.demandcapacitymgmt.demandcapacitymgmtbackend.exceptions.handler;

import java.util.ArrayList;
import java.util.List;
import lombok.extern.slf4j.Slf4j;
import org.eclipse.tractusx.demandcapacitymgmt.demandcapacitymgmtbackend.exceptions.BadRequestException;
import org.eclipse.tractusx.demandcapacitymgmt.demandcapacitymgmtbackend.exceptions.ExceptionResponse;
import org.eclipse.tractusx.demandcapacitymgmt.demandcapacitymgmtbackend.exceptions.NotFoundException;
import org.springframework.http.HttpStatus;
import org.eclipse.tractusx.demandcapacitymgmt.demandcapacitymgmtbackend.exceptions.base.ExceptionResponseImpl;
import org.eclipse.tractusx.demandcapacitymgmt.demandcapacitymgmtbackend.exceptions.type.BadRequestException;
import org.eclipse.tractusx.demandcapacitymgmt.demandcapacitymgmtbackend.exceptions.type.InternalServerErrorException;
import org.eclipse.tractusx.demandcapacitymgmt.demandcapacitymgmtbackend.exceptions.type.NotFoundException;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
Expand All @@ -38,13 +40,26 @@ public class RestExceptionHandler extends ResponseEntityExceptionHandler {

@ExceptionHandler(NotFoundException.class)
public final ResponseEntity<Object> handleNotFoundException(NotFoundException ex) {
ExceptionResponse exceptionResponse = new ExceptionResponse();
return ResponseEntity.status(HttpStatus.NOT_FOUND).body(exceptionResponse);
return ResponseEntity.status(ex.getCode()).body(new ExceptionResponseImpl<>(ex));
}

@ExceptionHandler(BadRequestException.class)
public final ResponseEntity<Object> handleBadRequestException(BadRequestException ex) {
ExceptionResponse exceptionResponse = new ExceptionResponse();
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(exceptionResponse);
return ResponseEntity.status(ex.getCode()).body(new ExceptionResponseImpl<>(ex));
}

@ExceptionHandler(Exception.class) //only used in 500
public final ResponseEntity<Object> handleInternalServerErrorException(Exception ex) {
return ResponseEntity
.status(500)
.body(
new ExceptionResponseImpl<>(
new InternalServerErrorException(
500,
"An internal server error has occurred",
new ArrayList<>(List.of("Localised error : " + ex.getLocalizedMessage()))
)
)
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,35 @@
* ********************************************************************************
*/

package org.eclipse.tractusx.demandcapacitymgmt.demandcapacitymgmtbackend.exceptions;
package org.eclipse.tractusx.demandcapacitymgmt.demandcapacitymgmtbackend.exceptions.type;

public class BadRequestException extends RuntimeException {
import java.util.List;
import org.eclipse.tractusx.demandcapacitymgmt.demandcapacitymgmtbackend.exceptions.base.CustomException;

public BadRequestException(String message) {
super(message);
public class BadRequestException extends RuntimeException implements CustomException<BadRequestException> {

private final int code;
private final String message;
private final List<String> details;

public BadRequestException(int code, String message, List<String> details) {
this.code = code;
this.message = message;
this.details = details;
}

@Override
public String getMessage() {
return message;
}

@Override
public int getCode() {
return code;
}

@Override
public List<String> getDetails() {
return details;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,37 @@
* ********************************************************************************
*/

package org.eclipse.tractusx.demandcapacitymgmt.demandcapacitymgmtbackend.exceptions;
package org.eclipse.tractusx.demandcapacitymgmt.demandcapacitymgmtbackend.exceptions.type;

public class NotFoundException extends RuntimeException {
import java.util.List;
import org.eclipse.tractusx.demandcapacitymgmt.demandcapacitymgmtbackend.exceptions.base.CustomException;

public NotFoundException(String message) {
super(message);
public class InternalServerErrorException
extends RuntimeException
implements CustomException<InternalServerErrorException> {

private final int code;
private final String message;
private final List<String> details;

public InternalServerErrorException(int code, String message, List<String> details) {
this.code = code;
this.message = message;
this.details = details;
}

@Override
public String getMessage() {
return message;
}

@Override
public int getCode() {
return code;
}

@Override
public List<String> getDetails() {
return details;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,35 @@
* ********************************************************************************
*/

package org.eclipse.tractusx.demandcapacitymgmt.demandcapacitymgmtbackend.exceptions;
package org.eclipse.tractusx.demandcapacitymgmt.demandcapacitymgmtbackend.exceptions.type;

import java.util.List;
import lombok.Getter;
import lombok.Setter;
import org.eclipse.tractusx.demandcapacitymgmt.demandcapacitymgmtbackend.exceptions.base.CustomException;

@Getter
@Setter
public class ExceptionResponse {
public class NotFoundException extends RuntimeException implements CustomException<NotFoundException> {

private static final long serialVersionUID = 1L;
private String code;
private String message;
private List<String> details;
private final int code;
private final String message;
private final List<String> details;

public NotFoundException(int code, String message, List<String> details) {
this.code = code;
this.message = message;
this.details = details;
}

@Override
public String getMessage() {
return message;
}

@Override
public int getCode() {
return code;
}

@Override
public List<String> getDetails() {
return details;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
import eclipse.tractusx.demand_capacity_mgmt_specification.model.CapacityGroupDefaultViewResponse;
import eclipse.tractusx.demand_capacity_mgmt_specification.model.CapacityGroupRequest;
import eclipse.tractusx.demand_capacity_mgmt_specification.model.CapacityGroupResponse;
import eclipse.tractusx.demand_capacity_mgmt_specification.model.MaterialDemandResponse;
import java.util.List;
import org.eclipse.tractusx.demandcapacitymgmt.demandcapacitymgmtbackend.entities.CapacityGroupEntity;
import org.eclipse.tractusx.demandcapacitymgmt.demandcapacitymgmtbackend.entities.enums.CapacityGroupStatus;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,6 @@

package org.eclipse.tractusx.demandcapacitymgmt.demandcapacitymgmtbackend.services;

import eclipse.tractusx.demand_capacity_mgmt_specification.model.DemandRequestDto;
import eclipse.tractusx.demand_capacity_mgmt_specification.model.DemandRequestUpdateDto;
import eclipse.tractusx.demand_capacity_mgmt_specification.model.DemandResponseDto;
import eclipse.tractusx.demand_capacity_mgmt_specification.model.MaterialDemandRequest;
import eclipse.tractusx.demand_capacity_mgmt_specification.model.MaterialDemandResponse;
import java.util.List;
Expand Down
Loading

0 comments on commit 7cb99e9

Please sign in to comment.