content: /*
   * Copyright 2017-2025 Hyland Software, Inc. and its affiliates.
   *
   * Licensed 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.activiti.cloud.services.error;
  
  import jakarta.servlet.http.HttpServletResponse;
  import org.activiti.api.model.shared.model.ActivitiErrorMessage;
  import org.activiti.api.runtime.model.impl.ActivitiErrorMessageImpl;
  import org.activiti.api.runtime.shared.NotFoundException;
  import org.activiti.core.common.spring.security.policies.ActivitiForbiddenException;
  import org.springframework.hateoas.EntityModel;
  import org.springframework.http.HttpStatus;
  import org.springframework.http.MediaType;
  import org.springframework.web.bind.annotation.ExceptionHandler;
  import org.springframework.web.bind.annotation.ResponseStatus;
  import org.springframework.web.bind.annotation.RestControllerAdvice;
  
  @RestControllerAdvice
  public class CommonExceptionHandler {
  
      @ExceptionHandler(ActivitiForbiddenException.class)
      @ResponseStatus(HttpStatus.FORBIDDEN)
      public EntityModel<ActivitiErrorMessage> handleAppException(
          ActivitiForbiddenException ex,
          HttpServletResponse response
      ) {
          response.setContentType(MediaType.APPLICATION_JSON_VALUE);
          return EntityModel.of(new ActivitiErrorMessageImpl(HttpStatus.FORBIDDEN.value(), ex.getMessage()));
      }
  
      @ExceptionHandler(IllegalStateException.class)
      @ResponseStatus(HttpStatus.BAD_REQUEST)
      public EntityModel<ActivitiErrorMessage> handleAppException(
          IllegalStateException ex,
          HttpServletResponse response
      ) {
          response.setContentType(MediaType.APPLICATION_JSON_VALUE);
          return EntityModel.of(new ActivitiErrorMessageImpl(HttpStatus.BAD_REQUEST.value(), ex.getMessage()));
      }
  
      @ExceptionHandler(NotFoundException.class)
      @ResponseStatus(HttpStatus.NOT_FOUND)
      public EntityModel<ActivitiErrorMessage> handleAppException(NotFoundException ex, HttpServletResponse response) {
          response.setContentType(MediaType.APPLICATION_JSON_VALUE);
          return EntityModel.of(new ActivitiErrorMessageImpl(HttpStatus.NOT_FOUND.value(), ex.getMessage()));
      }
  }
  