Skip to content

Commit

Permalink
Merge pull request #669 from hivemq/fix/http-errors
Browse files Browse the repository at this point in the history
Align error handling
  • Loading branch information
codepitbull authored Dec 11, 2024
2 parents 92911a6 + ef065f7 commit adc0c1d
Show file tree
Hide file tree
Showing 62 changed files with 2,435 additions and 1,294 deletions.
1,451 changes: 655 additions & 796 deletions ext/hivemq-edge-openapi-2024.8-SNAPSHOT.yaml

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,16 @@
package com.hivemq.api.error;

import com.fasterxml.jackson.databind.JsonMappingException;
import org.jetbrains.annotations.NotNull;
import com.hivemq.api.errors.InvalidInputError;
import com.hivemq.api.errors.ValidationError;
import com.hivemq.http.error.Error;
import com.hivemq.util.ErrorResponseUtil;
import org.jetbrains.annotations.NotNull;

import javax.annotation.Priority;
import javax.ws.rs.core.Response;
import javax.ws.rs.ext.ExceptionMapper;
import java.util.List;

@Priority(1)
public class CustomJsonMappingExceptionMapper implements ExceptionMapper<JsonMappingException> {
Expand All @@ -30,9 +34,9 @@ public class CustomJsonMappingExceptionMapper implements ExceptionMapper<JsonMap
public @NotNull Response toResponse(final @NotNull JsonMappingException exception) {
final String originalMessage = exception.getOriginalMessage();
if (originalMessage != null) {
return ErrorResponseUtil.invalidInput("Unable to parse JSON body: " + originalMessage);
return ErrorResponseUtil.errorResponse(new ValidationError(List.of(new Error(originalMessage, null))));
} else {
return ErrorResponseUtil.invalidInput("Unable to parse JSON body, please check the input format.");
return ErrorResponseUtil.errorResponse(new InvalidInputError("Unable to parse JSON body, please check the input format."));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,25 @@
package com.hivemq.api.error;

import com.fasterxml.jackson.core.JsonParseException;
import org.jetbrains.annotations.NotNull;
import com.hivemq.api.errors.InvalidInputError;
import com.hivemq.api.errors.ValidationError;
import com.hivemq.http.error.Error;
import com.hivemq.util.ErrorResponseUtil;
import org.jetbrains.annotations.NotNull;

import javax.ws.rs.core.Response;
import javax.ws.rs.ext.ExceptionMapper;
import java.util.List;

public class CustomJsonParseExceptionMapper implements ExceptionMapper<JsonParseException> {

@Override
public @NotNull Response toResponse(final JsonParseException exception) {
final String originalMessage = exception.getOriginalMessage();
if (originalMessage != null) {
return ErrorResponseUtil.invalidInput("Unable to parse JSON body: " + originalMessage);
return ErrorResponseUtil.errorResponse(new ValidationError(List.of(new Error(originalMessage, null))));
} else {
return ErrorResponseUtil.invalidInput("Unable to parse JSON body, please check the input format.");
return ErrorResponseUtil.errorResponse(new InvalidInputError("Unable to parse JSON body, please check the input format."));
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* Copyright 2019-present HiveMQ GmbH
*
* 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 com.hivemq.api.errors;

import com.hivemq.http.HttpStatus;
import com.hivemq.http.error.Error;
import com.hivemq.http.error.ProblemDetails;
import org.jetbrains.annotations.Nullable;

import java.util.List;

public class AlreadyExistsError extends ProblemDetails {
public AlreadyExistsError(
final @Nullable String error) {
super(
"ResourceAlreadyExists",
"The resource already exists",
"Requested to create a resource which already exists",
HttpStatus.CONFLICT_409,
List.of(new Error(error == null ? "An unexpected error occurred, check the logs" : error)));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* Copyright 2019-present HiveMQ GmbH
*
* 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 com.hivemq.api.errors;

import com.hivemq.http.HttpStatus;
import com.hivemq.http.error.Error;
import com.hivemq.http.error.ProblemDetails;
import org.jetbrains.annotations.Nullable;

import java.util.List;

public class BadRequestError extends ProblemDetails {
public BadRequestError(
final @Nullable String error) {
super(
"BadRequestError",
"Request could not be processed",
"Request could not be processed",
HttpStatus.BAD_REQUEST_400,
List.of(new Error(error)));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* Copyright 2019-present HiveMQ GmbH
*
* 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 com.hivemq.api.errors;

import com.hivemq.http.HttpStatus;
import com.hivemq.http.error.Error;
import com.hivemq.http.error.ProblemDetails;

import java.util.List;

public class BodyParameterMissingError extends ProblemDetails {
public BodyParameterMissingError(String parameterName) {
super(
"BodyParameterMissing",
"Required request body parameter missing",
"Required request body parameter missing",
HttpStatus.BAD_REQUEST_400,
List.of(new Error("Request body parameter missing: " + parameterName, parameterName)));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* Copyright 2019-present HiveMQ GmbH
*
* 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 com.hivemq.api.errors;

import com.hivemq.http.HttpStatus;
import com.hivemq.http.error.Error;
import com.hivemq.http.error.ProblemDetails;
import org.jetbrains.annotations.NotNull;

import java.util.List;

public class InsufficientStorageError extends ProblemDetails {
public InsufficientStorageError(
final @NotNull String cause) {
super(
"InsufficientStorage",
"Insufficient Storage",
"Insufficient Storage",
HttpStatus.INSUFFICIENT_STORAGE_507,
List.of(new Error(cause)));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* Copyright 2019-present HiveMQ GmbH
*
* 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 com.hivemq.api.errors;

import com.hivemq.http.HttpStatus;
import com.hivemq.http.error.Error;
import com.hivemq.http.error.ProblemDetails;
import org.jetbrains.annotations.Nullable;

import java.util.List;

public class InternalServerError extends ProblemDetails {
public InternalServerError(
final @Nullable String error) {
super(
"InternalServerError",
"InternalError",
"Internal error",
HttpStatus.INTERNAL_SERVER_ERROR_500,
List.of(new Error(error == null ? "An unexpected error occurred, check the logs" : error)));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* Copyright 2019-present HiveMQ GmbH
*
* 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 com.hivemq.api.errors;

import com.hivemq.http.HttpStatus;
import com.hivemq.http.error.Error;
import com.hivemq.http.error.ProblemDetails;
import org.jetbrains.annotations.Nullable;

import java.util.List;

public class InvalidInputError extends ProblemDetails {
public InvalidInputError(
final @Nullable String error) {
super(
"InvalidInputError",
"Invalid input",
"JSON failed validation.",
HttpStatus.BAD_REQUEST_400,
List.of(new Error("Unparseable JSON: " + error, null)));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* Copyright 2019-present HiveMQ GmbH
*
* 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 com.hivemq.api.errors;

import com.hivemq.http.HttpStatus;
import com.hivemq.http.error.Error;
import com.hivemq.http.error.ProblemDetails;

import java.util.List;

public class InvalidQueryParameterError extends ProblemDetails {
public InvalidQueryParameterError(String parameterName, String reason) {
super(
"InvalidQueryParameter",
"Query parameter is invalid",
"Query parameter is invalid",
HttpStatus.BAD_REQUEST_400,
List.of(new Error(String.format("Query parameter %s is invalid: %s", parameterName, reason), parameterName)));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* Copyright 2019-present HiveMQ GmbH
*
* 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 com.hivemq.api.errors;

import com.hivemq.http.HttpStatus;
import com.hivemq.http.error.Error;
import com.hivemq.http.error.ProblemDetails;

import java.util.List;

public class InvalidQueryParameterErrors extends ProblemDetails {
public InvalidQueryParameterErrors(List<Error> errors) {
super(
"InvalidQueryParameter",
"Query parameter is invalid",
"Query parameter is invalid",
HttpStatus.BAD_REQUEST_400,
errors);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
* Copyright 2019-present HiveMQ GmbH
*
* 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 com.hivemq.api.errors;

import com.hivemq.http.HttpStatus;
import com.hivemq.http.error.ProblemDetails;

import java.util.List;

public class MethodNotAllowedError extends ProblemDetails {
public MethodNotAllowedError() {
super(
"MethodNotAllowedError",
"Method not allowed",
"Method not allowed",
HttpStatus.METHOD_NOT_ALLOWED_405,
List.of());
}
}
Loading

0 comments on commit adc0c1d

Please sign in to comment.