|
| 1 | +/* |
| 2 | + * Copyright 2024-2026 Firefly Software Solutions Inc |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | + |
| 18 | +package org.fireflyframework.web.error.examples; |
| 19 | + |
| 20 | +import org.fireflyframework.web.error.converter.ExceptionConverterService; |
| 21 | +import org.fireflyframework.web.error.exceptions.*; |
| 22 | +import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; |
| 23 | +import org.springframework.dao.DataIntegrityViolationException; |
| 24 | +import org.springframework.dao.OptimisticLockingFailureException; |
| 25 | +import org.springframework.stereotype.Service; |
| 26 | +import org.springframework.web.client.HttpClientErrorException; |
| 27 | +import org.springframework.web.client.HttpServerErrorException; |
| 28 | + |
| 29 | +import java.util.concurrent.TimeoutException; |
| 30 | + |
| 31 | +/** |
| 32 | + * Example service that demonstrates how to use the exception handling features. |
| 33 | + * This class shows how to throw business exceptions directly and how to use |
| 34 | + * the exception conversion mechanism. |
| 35 | + */ |
| 36 | +@Service |
| 37 | +@ConditionalOnClass({DataIntegrityViolationException.class, OptimisticLockingFailureException.class}) |
| 38 | +public class ExceptionHandlingExample { |
| 39 | + |
| 40 | + private final ExceptionConverterService converterService; |
| 41 | + |
| 42 | + /** |
| 43 | + * Creates a new ExceptionHandlingExample with the given converter service. |
| 44 | + * |
| 45 | + * @param converterService the exception converter service |
| 46 | + */ |
| 47 | + public ExceptionHandlingExample(ExceptionConverterService converterService) { |
| 48 | + this.converterService = converterService; |
| 49 | + } |
| 50 | + |
| 51 | + /** |
| 52 | + * Example of throwing business exceptions directly. |
| 53 | + * This method demonstrates how to create and throw various business exceptions. |
| 54 | + * |
| 55 | + * @param exceptionType the type of exception to throw |
| 56 | + * @throws BusinessException the business exception |
| 57 | + */ |
| 58 | + public void throwBusinessException(String exceptionType) throws BusinessException { |
| 59 | + switch (exceptionType) { |
| 60 | + case "resource-not-found": |
| 61 | + throw ResourceNotFoundException.forResource("User", "123"); |
| 62 | + case "invalid-request": |
| 63 | + throw InvalidRequestException.forField("email", "invalid-email", "must be a valid email format"); |
| 64 | + case "conflict": |
| 65 | + throw ConflictException.resourceAlreadyExists("User", "john.doe@example.com"); |
| 66 | + case "unauthorized": |
| 67 | + throw UnauthorizedException.missingAuthentication(); |
| 68 | + case "forbidden": |
| 69 | + throw ForbiddenException.insufficientPermissions("ADMIN"); |
| 70 | + case "service-error": |
| 71 | + throw ServiceException.withCause("Failed to process request", new RuntimeException("Database connection failed")); |
| 72 | + case "validation-error": |
| 73 | + ValidationException.Builder validationBuilder = new ValidationException.Builder() |
| 74 | + .addError("email", "must be a valid email") |
| 75 | + .addError("password", "must be at least 8 characters"); |
| 76 | + throw validationBuilder.build(); |
| 77 | + case "third-party-service": |
| 78 | + throw ThirdPartyServiceException.serviceUnavailable("PaymentService"); |
| 79 | + case "rate-limit": |
| 80 | + throw RateLimitException.forUser("user123", 60); |
| 81 | + case "data-integrity": |
| 82 | + throw DataIntegrityException.uniqueConstraintViolation("email", "john.doe@example.com"); |
| 83 | + case "operation-timeout": |
| 84 | + throw OperationTimeoutException.databaseTimeout("query", 5000); |
| 85 | + case "concurrency": |
| 86 | + throw ConcurrencyException.optimisticLockingFailure("User", "123"); |
| 87 | + case "authorization": |
| 88 | + throw AuthorizationException.missingPermission("USER_WRITE"); |
| 89 | + default: |
| 90 | + throw new BusinessException("Unknown exception type: " + exceptionType); |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + /** |
| 95 | + * Example of manually converting standard exceptions to business exceptions. |
| 96 | + * This method demonstrates how to use the converter service to convert exceptions. |
| 97 | + * |
| 98 | + * @param exceptionType the type of exception to convert |
| 99 | + * @return the converted business exception |
| 100 | + */ |
| 101 | + public BusinessException convertStandardException(String exceptionType) { |
| 102 | + Throwable exception; |
| 103 | + |
| 104 | + switch (exceptionType) { |
| 105 | + case "data-integrity": |
| 106 | + exception = new DataIntegrityViolationException("Duplicate entry 'john.doe@example.com' for key 'email'"); |
| 107 | + break; |
| 108 | + case "optimistic-locking": |
| 109 | + exception = new OptimisticLockingFailureException("Failed to update entity User with id '123' - it was modified by another transaction"); |
| 110 | + break; |
| 111 | + case "client-error": |
| 112 | + exception = HttpClientErrorException.NotFound.create(org.springframework.http.HttpStatus.NOT_FOUND, "Not Found", null, null, null); |
| 113 | + break; |
| 114 | + case "server-error": |
| 115 | + exception = HttpServerErrorException.ServiceUnavailable.create(org.springframework.http.HttpStatus.SERVICE_UNAVAILABLE, "Service Unavailable", null, null, null); |
| 116 | + break; |
| 117 | + case "timeout": |
| 118 | + exception = new TimeoutException("Operation timed out after 5000ms"); |
| 119 | + break; |
| 120 | + default: |
| 121 | + exception = new RuntimeException("Unknown exception: " + exceptionType); |
| 122 | + break; |
| 123 | + } |
| 124 | + |
| 125 | + return converterService.convertException(exception); |
| 126 | + } |
| 127 | + |
| 128 | + /** |
| 129 | + * Example of automatically converting standard exceptions to business exceptions. |
| 130 | + * Any exceptions thrown by this method will be automatically converted to business exceptions |
| 131 | + * by the GlobalExceptionHandler. |
| 132 | + * |
| 133 | + * @param exceptionType the type of exception to throw |
| 134 | + * @throws BusinessException the converted business exception |
| 135 | + * @throws TimeoutException if a timeout occurs |
| 136 | + */ |
| 137 | + public void throwStandardException(String exceptionType) throws BusinessException, TimeoutException { |
| 138 | + switch (exceptionType) { |
| 139 | + case "data-integrity": |
| 140 | + throw new DataIntegrityViolationException("Duplicate entry 'john.doe@example.com' for key 'email'"); |
| 141 | + case "optimistic-locking": |
| 142 | + throw new OptimisticLockingFailureException("Failed to update entity User with id '123' - it was modified by another transaction"); |
| 143 | + case "client-error": |
| 144 | + throw HttpClientErrorException.NotFound.create(org.springframework.http.HttpStatus.NOT_FOUND, "Not Found", null, null, null); |
| 145 | + case "server-error": |
| 146 | + throw HttpServerErrorException.ServiceUnavailable.create(org.springframework.http.HttpStatus.SERVICE_UNAVAILABLE, "Service Unavailable", null, null, null); |
| 147 | + case "timeout": |
| 148 | + throw new TimeoutException("Operation timed out after 5000ms"); |
| 149 | + default: |
| 150 | + throw new RuntimeException("Unknown exception: " + exceptionType); |
| 151 | + } |
| 152 | + } |
| 153 | +} |
0 commit comments