Skip to content

Commit

Permalink
include message when throwing exceptions from BuildController
Browse files Browse the repository at this point in the history
  • Loading branch information
anotherchrisberry committed Jan 7, 2016
1 parent cc0125a commit 2cfed64
Showing 1 changed file with 37 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import com.netflix.spinnaker.igor.jenkins.client.model.JobConfig
import com.netflix.spinnaker.igor.jenkins.client.model.QueuedJob
import groovy.transform.InheritConstructors
import groovy.util.logging.Slf4j
import retrofit.RetrofitError
import javax.servlet.http.HttpServletRequest
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.http.HttpStatus
Expand Down Expand Up @@ -54,6 +55,7 @@ class BuildController {
def job = (String) request.getAttribute(
HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE).split('/').drop(5).join('/')
if (!masters.map.containsKey(master)) {
log.error("Master '${master}' not found")
throw new MasterNotFoundException()
}
Map result = objectMapper.convertValue(masters.map[master].getBuild(job, buildNumber), Map)
Expand All @@ -67,7 +69,7 @@ class BuildController {
}

}
}catch(Exception e){
} catch (Exception e) {
log.error("could not get scm results for $master / $job / $buildNumber")
}
result
Expand All @@ -76,16 +78,26 @@ class BuildController {
@RequestMapping(value = '/builds/queue/{master}/{item}')
QueuedJob getQueueLocation(@PathVariable String master, @PathVariable int item){
if (!masters.map.containsKey(master)) {
log.error("Master '${master}' not found")
throw new MasterNotFoundException()
}
masters.map[master].getQueuedItem(item)
try {
return masters.map[master].getQueuedItem(item)
} catch (RetrofitError e) {
if (e.response?.status == HttpStatus.NOT_FOUND.value()) {
log.error("Queued job '${item}' not found for master '${master}'.")
throw new QueuedJobNotFoundException()
}
throw e
}
}

@RequestMapping(value = '/builds/all/{master}/**')
List<Build> getBuilds(@PathVariable String master, HttpServletRequest request) {
def job = (String) request.getAttribute(
HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE).split('/').drop(4).join('/')
if (!masters.map.containsKey(master)) {
log.error("Master '${master}' not found")
throw new MasterNotFoundException()
}
masters.map[master].getBuilds(job).list
Expand All @@ -98,6 +110,7 @@ class BuildController {
def job = (String) request.getAttribute(
HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE).split('/').drop(4).join('/')
if (!masters.map.containsKey(master)) {
log.error("Master '${master}' not found")
throw new MasterNotFoundException()
}

Expand All @@ -119,12 +132,14 @@ class BuildController {
}

if (response.status != 201) {
log.error("Received a non-201 status when submitting job '${job}' to master '${master}'")
throw new BuildJobError()
}

log.info("Submitted build job `${job}`")
def locationHeader = response.headers.find { it.name == "Location" }
if (!locationHeader) {
log.error("Could not find Location header for job '${job}'")
throw new QueuedJobDeterminationError()
}
def queuedLocation = locationHeader.value
Expand All @@ -139,6 +154,7 @@ class BuildController {
def job = (String) request.getAttribute(
HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE).split('/').drop(6).join('/')
if (!masters.map.containsKey(master)) {
log.error("Could not find master '${master}' to get properties")
throw new MasterNotFoundException()
}
Map<String, Object> map = [:]
Expand Down Expand Up @@ -170,6 +186,11 @@ class BuildController {
@InheritConstructors
static class MasterNotFoundException extends RuntimeException {}

@ResponseStatus(value = HttpStatus.NOT_FOUND, reason = "Queued job not found!")
@InheritConstructors
static class QueuedJobNotFoundException extends RuntimeException {}


@ResponseStatus(value = HttpStatus.BAD_REQUEST, reason = "Build job failed!")
@InheritConstructors
static class BuildJobError extends RuntimeException {}
Expand All @@ -183,6 +204,7 @@ class BuildController {
@RequestMapping(value = '/jobs/{master}/{job}/{buildNumber}')
Map getJobStatusLegacy(@PathVariable String master, @PathVariable String job, @PathVariable Integer buildNumber) {
if (!masters.map.containsKey(master)) {
log.error("Could not find master '${master}' to get job status for job '${job}'")
throw new MasterNotFoundException()
}
Map result = objectMapper.convertValue(masters.map[master].getBuild(job, buildNumber), Map)
Expand All @@ -196,7 +218,7 @@ class BuildController {
}

}
}catch(Exception e){
} catch (Exception e) {
log.error("could not get scm results for $master / $job / $buildNumber")
}
result
Expand All @@ -205,14 +227,24 @@ class BuildController {
@RequestMapping(value = '/jobs/{master}/queue/{item}')
QueuedJob getQueueLocationLegacy(@PathVariable String master, @PathVariable int item){
if (!masters.map.containsKey(master)) {
log.error("Master '${master}' not found for item '${item}'")
throw new MasterNotFoundException()
}
masters.map[master].getQueuedItem(item)
try {
return masters.map[master].getQueuedItem(item)
} catch (RetrofitError e) {
if (e.response?.status == HttpStatus.NOT_FOUND.value()) {
log.error("Queued job '${item}' not found for master '${master}'.")
throw new QueuedJobNotFoundException()
}
throw e
}
}

@RequestMapping(value = '/jobs/{master}/{job}/builds')
List<Build> getBuildsLegacy(@PathVariable String master, @PathVariable String job) {
if (!masters.map.containsKey(master)) {
log.error("Master '${master}' not found for job '${job}'")
throw new MasterNotFoundException()
}
masters.map[master].getBuilds(job).list
Expand All @@ -223,6 +255,7 @@ class BuildController {
@PathVariable String master,
@PathVariable String job, @PathVariable Integer buildNumber, @PathVariable String fileName) {
if (!masters.map.containsKey(master)) {
log.error("Master '${master}' not found for job '${job}'")
throw new MasterNotFoundException()
}
Map<String, Object> map = [:]
Expand Down

0 comments on commit 2cfed64

Please sign in to comment.