All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.eclipse.digitaltwin.basyx.http.BaSyxExceptionHandler Maven / Gradle / Ivy

/*******************************************************************************
 * Copyright (C) 2023 the Eclipse BaSyx Authors
 * 
 * Permission is hereby granted, free of charge, to any person obtaining
 * a copy of this software and associated documentation files (the
 * "Software"), to deal in the Software without restriction, including
 * without limitation the rights to use, copy, modify, merge, publish,
 * distribute, sublicense, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject to
 * the following conditions:
 * 
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 * 
 * SPDX-License-Identifier: MIT
 ******************************************************************************/


package org.eclipse.digitaltwin.basyx.http;

import org.eclipse.digitaltwin.basyx.core.exceptions.AssetLinkDoesNotExistException;
import org.eclipse.digitaltwin.basyx.core.exceptions.CollidingAssetLinkException;
import org.eclipse.digitaltwin.basyx.core.exceptions.CollidingIdentifierException;
import org.eclipse.digitaltwin.basyx.core.exceptions.MissingIdentifierException;
import org.eclipse.digitaltwin.basyx.core.exceptions.ElementDoesNotExistException;
import org.eclipse.digitaltwin.basyx.core.exceptions.ElementNotAFileException;
import org.eclipse.digitaltwin.basyx.core.exceptions.FeatureNotSupportedException;
import org.eclipse.digitaltwin.basyx.core.exceptions.FileDoesNotExistException;
import org.eclipse.digitaltwin.basyx.core.exceptions.IdentificationMismatchException;
import org.eclipse.digitaltwin.basyx.core.exceptions.InsufficientPermissionException;
import org.eclipse.digitaltwin.basyx.core.exceptions.NotInvokableException;
import org.eclipse.digitaltwin.basyx.core.exceptions.NullSubjectException;
import org.eclipse.digitaltwin.basyx.core.exceptions.OperationDelegationException;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.context.request.WebRequest;
import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler;

/**
 * Configures overall Exception to HTTP status code mapping
 * 
 * @author schnicke
 *
 */
@ControllerAdvice
public class BaSyxExceptionHandler extends ResponseEntityExceptionHandler {

	@ExceptionHandler(ElementDoesNotExistException.class)
	public  ResponseEntity handleElementNotFoundException(ElementDoesNotExistException exception, WebRequest request) {
		return new ResponseEntity<>(HttpStatus.NOT_FOUND);
	}
	
	@ExceptionHandler(AssetLinkDoesNotExistException.class)
	public  ResponseEntity handleElementNotFoundException(AssetLinkDoesNotExistException exception, WebRequest request) {
		return new ResponseEntity<>(HttpStatus.NOT_FOUND);
	}
	
	@ExceptionHandler(FileDoesNotExistException.class)
	public  ResponseEntity handleElementNotFoundException(FileDoesNotExistException exception, WebRequest request) {
		return new ResponseEntity<>(HttpStatus.NOT_FOUND);
	}

	@ExceptionHandler(CollidingIdentifierException.class)
	public  ResponseEntity handleCollidingIdentifierException(CollidingIdentifierException exception, WebRequest request) {
		return new ResponseEntity<>(HttpStatus.CONFLICT);
	}
	
	@ExceptionHandler(MissingIdentifierException.class)
	public  ResponseEntity handleMissingIdentifierException(MissingIdentifierException exception, WebRequest request) {
		return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
	}
	
	@ExceptionHandler(CollidingAssetLinkException.class)
	public  ResponseEntity handleCollidingIdentifierException(CollidingAssetLinkException exception, WebRequest request) {
		return new ResponseEntity<>(HttpStatus.CONFLICT);
	}

	@ExceptionHandler(IllegalArgumentException.class)
	public  ResponseEntity handleIllegalArgumentException(IllegalArgumentException exception) {
		return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
	}
	
	@ExceptionHandler(IdentificationMismatchException.class)
	public  ResponseEntity handleIdMismatchException(IdentificationMismatchException exception) {
		return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
	}

	@ExceptionHandler(FeatureNotSupportedException.class)
	public  ResponseEntity handleFeatureNotSupportedException(FeatureNotSupportedException exception) {
		return new ResponseEntity<>(HttpStatus.NOT_IMPLEMENTED);
	}

	@ExceptionHandler(NotInvokableException.class)
	public  ResponseEntity handleNotInvokableException(NotInvokableException exception) {
		return new ResponseEntity<>(HttpStatus.METHOD_NOT_ALLOWED);
	}
	
	@ExceptionHandler(ElementNotAFileException.class)
	public  ResponseEntity handleElementNotAFileException(ElementNotAFileException exception) {
		return new ResponseEntity<>(HttpStatus.PRECONDITION_FAILED);
	}
	
	@ExceptionHandler(InsufficientPermissionException.class)
	public  ResponseEntity handleInsufficientPermissionException(InsufficientPermissionException exception, WebRequest request) {
		return new ResponseEntity<>(HttpStatus.FORBIDDEN);
	}
	
	@ExceptionHandler(NullSubjectException.class)
	public  ResponseEntity handleNullSubjectException(NullSubjectException exception) {
		return new ResponseEntity<>(HttpStatus.UNAUTHORIZED);
	}
	
	@ExceptionHandler(OperationDelegationException.class)
	public  ResponseEntity handleNullSubjectException(OperationDelegationException exception) {
		return new ResponseEntity<>(HttpStatus.FAILED_DEPENDENCY);
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy