Skip to content

Commit f8913ea

Browse files
authored
Return 503 (instead of 500) when trying to deploy a target that is no ready (#536)
* Return 503 (instead of 500) when trying to deploy a target that is not ready * Add declared exceptions to method comments
1 parent 7e65657 commit f8913ea

4 files changed

Lines changed: 32 additions & 10 deletions

File tree

src/main/api/deployer-api.yaml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,8 @@ paths:
223223
$ref: '#/components/responses/NotFound'
224224
'500':
225225
$ref: '#/components/responses/InternalServerError'
226+
'503':
227+
$ref: '#/components/responses/ServiceUnavailable'
226228

227229
/api/1/target/deploy/{env}/{site_name}:
228230
post:
@@ -1041,6 +1043,16 @@ components:
10411043
type: string
10421044
example: Internal Server Error
10431045

1046+
ServiceUnavailable:
1047+
description: Service unavailable
1048+
content:
1049+
application/json:
1050+
schema:
1051+
type: object
1052+
properties:
1053+
message:
1054+
type: string
1055+
example: Not available
10441056
Accepted:
10451057
description: Accepted
10461058
content:

src/main/java/org/craftercms/deployer/api/DeploymentService.java

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (C) 2007-2022 Crafter Software Corporation. All Rights Reserved.
2+
* Copyright (C) 2007-2026 Crafter Software Corporation. All Rights Reserved.
33
*
44
* This program is free software: you can redistribute it and/or modify
55
* it under the terms of the GNU General Public License version 3 as published by
@@ -15,11 +15,12 @@
1515
*/
1616
package org.craftercms.deployer.api;
1717

18-
import java.util.List;
19-
import java.util.Map;
20-
2118
import org.craftercms.deployer.api.exceptions.DeploymentServiceException;
2219
import org.craftercms.deployer.api.exceptions.TargetNotFoundException;
20+
import org.craftercms.deployer.api.exceptions.TargetNotReadyException;
21+
22+
import java.util.List;
23+
import java.util.Map;
2324

2425
/**
2526
* Service for doing deployments.
@@ -46,9 +47,11 @@ public interface DeploymentService {
4647
* @param waitTillDone if the method should wait till the deployment is done or return immediately
4748
* @param params additional parameters that can be used by the deployment processors
4849
* @return the deployment info
50+
* @throws TargetNotFoundException if the target for the specified env and site name was not found
4951
* @throws DeploymentServiceException if there was an error while executing the deployments
52+
* @throws TargetNotReadyException if the target is not ready to accept deployments
5053
*/
5154
Deployment deployTarget(String env, String siteName, boolean waitTillDone,
52-
Map<String, Object> params) throws TargetNotFoundException, DeploymentServiceException;
55+
Map<String, Object> params) throws TargetNotFoundException, DeploymentServiceException, TargetNotReadyException;
5356

5457
}

src/main/java/org/craftercms/deployer/impl/DeploymentServiceImpl.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (C) 2007-2023 Crafter Software Corporation. All Rights Reserved.
2+
* Copyright (C) 2007-2026 Crafter Software Corporation. All Rights Reserved.
33
*
44
* This program is free software: you can redistribute it and/or modify
55
* it under the terms of the GNU General Public License version 3 as published by
@@ -82,11 +82,11 @@ public List<Deployment> deployAllTargets(boolean waitTillDone,
8282

8383
@Override
8484
public Deployment deployTarget(String env, String siteName, boolean waitTillDone,
85-
Map<String, Object> params) throws TargetNotFoundException,
86-
DeploymentServiceException {
85+
Map<String, Object> params) throws TargetNotFoundException,
86+
DeploymentServiceException, TargetNotReadyException {
8787
try {
8888
return targetService.getTarget(env, siteName).deploy(waitTillDone, params);
89-
} catch (TargetServiceException | TargetNotReadyException e) {
89+
} catch (TargetServiceException e) {
9090
throw new DeploymentServiceException(format("Error while deploying target '%s'", TargetImpl.getId(env, siteName)), e);
9191
}
9292
}

src/main/java/org/craftercms/deployer/impl/rest/ExceptionHandlers.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (C) 2007-2023 Crafter Software Corporation. All Rights Reserved.
2+
* Copyright (C) 2007-2026 Crafter Software Corporation. All Rights Reserved.
33
*
44
* This program is free software: you can redistribute it and/or modify
55
* it under the terms of the GNU General Public License version 3 as published by
@@ -23,6 +23,7 @@
2323
import org.craftercms.core.controller.rest.ValidationFieldError;
2424
import org.craftercms.deployer.api.exceptions.TargetAlreadyExistsException;
2525
import org.craftercms.deployer.api.exceptions.TargetNotFoundException;
26+
import org.craftercms.deployer.api.exceptions.TargetNotReadyException;
2627
import org.springframework.core.Ordered;
2728
import org.springframework.core.annotation.Order;
2829
import org.springframework.http.HttpHeaders;
@@ -96,6 +97,12 @@ public ResponseEntity<Object> handleInvalidManagementTokenException(InvalidManag
9697
request);
9798
}
9899

100+
@ExceptionHandler(TargetNotReadyException.class)
101+
public ResponseEntity<Object> handleTargetNotReadyException(TargetNotReadyException ex, WebRequest request) {
102+
return handleExceptionInternal(ex, ex.getMessage(), new HttpHeaders(), HttpStatus.SERVICE_UNAVAILABLE,
103+
request);
104+
}
105+
99106
@Override
100107
protected ResponseEntity<Object> handleMethodArgumentNotValid(MethodArgumentNotValidException ex, HttpHeaders headers,
101108
HttpStatusCode status, WebRequest request) {

0 commit comments

Comments
 (0)