Search in sources :

Example 21 with HttpStatus

use of org.springframework.http.HttpStatus in project FP-PSP-SERVER by FundacionParaguaya.

the class ExceptionTranslatorAdvice method handleGeneralUncaughtExcption.

/**
 * <p>
 *     Any other unhandled exceptions are processed here.
 * </p>
 * <p>
 *     We also process user defined (non standard) exceptions annotated with @ResponseStatus.
 *     See {@link py.org.fundacionparaguaya.pspserver.common.exceptions.UnknownResourceException}
 * </p>
 * @param ex
 * @return
 */
@ExceptionHandler(Exception.class)
protected ResponseEntity<Object> handleGeneralUncaughtExcption(Exception ex) {
    HttpStatus status;
    ErrorDTO errorDto;
    ResponseStatus responseStatus = AnnotationUtils.findAnnotation(ex.getClass(), ResponseStatus.class);
    if (responseStatus != null) {
        status = responseStatus.value();
        errorDto = ErrorDTO.of(responseStatus.reason(), ex.getLocalizedMessage()).formatWithCode();
    } else {
        status = HttpStatus.INTERNAL_SERVER_ERROR;
        errorDto = ErrorDTO.of(ErrorCodes.INTERNAL_SERVER_ERROR.getMessage(), ex.getLocalizedMessage()).formatWithCode();
    }
    LOG.error("Error id: {}", errorDto.getErrorId());
    LOG.error(ex.getMessage(), ex);
    return generateResponseEntity(errorDto, status);
}
Also used : HttpStatus(org.springframework.http.HttpStatus) ResponseStatus(org.springframework.web.bind.annotation.ResponseStatus) FieldErrorDTO(py.org.fundacionparaguaya.pspserver.common.dtos.FieldErrorDTO) ErrorDTO(py.org.fundacionparaguaya.pspserver.common.dtos.ErrorDTO) ExceptionHandler(org.springframework.web.bind.annotation.ExceptionHandler)

Example 22 with HttpStatus

use of org.springframework.http.HttpStatus in project components by Talend.

the class RuntimeControllerImpl method doValidateDatastoreConnection.

private ResponseEntity<ValidationResultsDto> doValidateDatastoreConnection(DatastoreProperties properties) {
    DatastoreDefinition<DatastoreProperties> definition = propertiesHelpers.getFirstDefinitionFromProperties(properties);
    try (SandboxedInstance instance = RuntimeUtil.createRuntimeClass(definition.getRuntimeInfo(properties), properties.getClass().getClassLoader())) {
        DatastoreRuntime<DatastoreProperties> datastoreRuntime = (DatastoreRuntime) instance.getInstance();
        datastoreRuntime.initialize(null, properties);
        Iterable<ValidationResult> healthChecks = datastoreRuntime.doHealthChecks(null);
        ValidationResultsDto response = new ValidationResultsDto(healthChecks == null ? emptyList() : newArrayList(healthChecks));
        HttpStatus httpStatus = response.getStatus() == ValidationResult.Result.OK ? HttpStatus.OK : HttpStatus.BAD_REQUEST;
        return new ResponseEntity<>(response, httpStatus);
    }
}
Also used : SandboxedInstance(org.talend.daikon.sandbox.SandboxedInstance) DatastoreProperties(org.talend.components.common.datastore.DatastoreProperties) ResponseEntity(org.springframework.http.ResponseEntity) HttpStatus(org.springframework.http.HttpStatus) DatastoreRuntime(org.talend.components.common.datastore.runtime.DatastoreRuntime) ValidationResultsDto(org.talend.components.service.rest.dto.ValidationResultsDto) ValidationResult(org.talend.daikon.properties.ValidationResult)

Example 23 with HttpStatus

use of org.springframework.http.HttpStatus in project crnk-framework by crnk-project.

the class CrnkErrorController method errorToJsonApi.

// TODO for whatever reason this is not called directly
@RequestMapping(produces = HttpHeaders.JSONAPI_CONTENT_TYPE)
@ResponseBody
public ResponseEntity<Document> errorToJsonApi(HttpServletRequest request) {
    Map<String, Object> body = getErrorAttributes(request, isIncludeStackTrace(request, MediaType.ALL));
    HttpStatus status = getStatus(request);
    ErrorDataBuilder errorDataBuilder = ErrorData.builder();
    for (Map.Entry<String, Object> attribute : body.entrySet()) {
        if (attribute.getKey().equals("status")) {
            errorDataBuilder.setStatus(attribute.getValue().toString());
        } else if (attribute.getKey().equals("error")) {
            errorDataBuilder.setTitle(attribute.getValue().toString());
        } else if (attribute.getKey().equals("message")) {
            errorDataBuilder.setDetail(attribute.getValue().toString());
        } else {
            errorDataBuilder.addMetaField(attribute.getKey(), attribute.getValue());
        }
    }
    Document document = new Document();
    document.setErrors(Arrays.asList(errorDataBuilder.build()));
    return new ResponseEntity<>(document, status);
}
Also used : ErrorDataBuilder(io.crnk.core.engine.document.ErrorDataBuilder) ResponseEntity(org.springframework.http.ResponseEntity) HttpStatus(org.springframework.http.HttpStatus) Document(io.crnk.core.engine.document.Document) Map(java.util.Map) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) ResponseBody(org.springframework.web.bind.annotation.ResponseBody)

Example 24 with HttpStatus

use of org.springframework.http.HttpStatus in project tutorials by eugenp.

the class EmployeeRestController method createEmployee.

@PostMapping("/employees")
public ResponseEntity<Employee> createEmployee(@RequestBody Employee employee) {
    HttpStatus status = HttpStatus.CREATED;
    Employee saved = employeeService.save(employee);
    return new ResponseEntity<>(saved, status);
}
Also used : ResponseEntity(org.springframework.http.ResponseEntity) HttpStatus(org.springframework.http.HttpStatus) PostMapping(org.springframework.web.bind.annotation.PostMapping)

Example 25 with HttpStatus

use of org.springframework.http.HttpStatus in project tutorials by eugenp.

the class StepDefsIntegrationTest method the_client_receives_status_code_of.

@Then("^the client receives status code of (\\d+)$")
public void the_client_receives_status_code_of(int statusCode) throws Throwable {
    final HttpStatus currentStatusCode = latestResponse.getTheResponse().getStatusCode();
    assertThat("status code is incorrect : " + latestResponse.getBody(), currentStatusCode.value(), is(statusCode));
}
Also used : HttpStatus(org.springframework.http.HttpStatus) Then(cucumber.api.java.en.Then)

Aggregations

HttpStatus (org.springframework.http.HttpStatus)161 ResponseEntity (org.springframework.http.ResponseEntity)39 HttpHeaders (org.springframework.http.HttpHeaders)35 Test (org.junit.jupiter.api.Test)26 MediaType (org.springframework.http.MediaType)17 Mono (reactor.core.publisher.Mono)17 IOException (java.io.IOException)16 ExceptionHandler (org.springframework.web.bind.annotation.ExceptionHandler)14 Collections (java.util.Collections)13 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)13 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)13 URI (java.net.URI)12 List (java.util.List)11 Optional (java.util.Optional)11 Test (org.junit.Test)11 Map (java.util.Map)10 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)9 ClassPathResource (org.springframework.core.io.ClassPathResource)9 Resource (org.springframework.core.io.Resource)9 HashMap (java.util.HashMap)8