From 0f0a36986df33eb0025d8f1fd162ae41ea79abc1 Mon Sep 17 00:00:00 2001 From: "tsaitsung-han.tht" Date: Wed, 11 Dec 2024 11:34:16 +0800 Subject: [PATCH] Validate message empty body for C# SDK --- .../Error/PayloadEmptyException.cs | 34 +++++++++++++++++++ csharp/rocketmq-client-csharp/Message.cs | 2 +- .../rocketmq-client-csharp/StatusChecker.cs | 2 ++ 3 files changed, 37 insertions(+), 1 deletion(-) create mode 100644 csharp/rocketmq-client-csharp/Error/PayloadEmptyException.cs diff --git a/csharp/rocketmq-client-csharp/Error/PayloadEmptyException.cs b/csharp/rocketmq-client-csharp/Error/PayloadEmptyException.cs new file mode 100644 index 000000000..0db1be9cc --- /dev/null +++ b/csharp/rocketmq-client-csharp/Error/PayloadEmptyException.cs @@ -0,0 +1,34 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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. + */ + +namespace Org.Apache.Rocketmq.Error +{ + /// + /// Generic exception represents when the request entity is empty. + /// + public class PayloadEmptyException : ClientException + { + public PayloadEmptyException(int responseCode, string requestId, string message) : base(responseCode, + requestId, message) + { + } + + public PayloadEmptyException(int responseCode, string message) : base(responseCode, message) + { + } + } +} \ No newline at end of file diff --git a/csharp/rocketmq-client-csharp/Message.cs b/csharp/rocketmq-client-csharp/Message.cs index bbd2c20ea..c1e2094e7 100644 --- a/csharp/rocketmq-client-csharp/Message.cs +++ b/csharp/rocketmq-client-csharp/Message.cs @@ -91,7 +91,7 @@ public Builder SetTopic(string topic) public Builder SetBody(byte[] body) { - Preconditions.CheckArgument(null != body, "body should not be null"); + Preconditions.CheckArgument(null != body || body.Length == 0, "body should not be empty"); _body = body; return this; } diff --git a/csharp/rocketmq-client-csharp/StatusChecker.cs b/csharp/rocketmq-client-csharp/StatusChecker.cs index 12d12fe68..e8764f2ca 100644 --- a/csharp/rocketmq-client-csharp/StatusChecker.cs +++ b/csharp/rocketmq-client-csharp/StatusChecker.cs @@ -77,6 +77,8 @@ public static void Check(Proto.Status status, IMessage request, string requestId case Proto.Code.PayloadTooLarge: case Proto.Code.MessageBodyTooLarge: throw new PayloadTooLargeException((int)statusCode, requestId, statusMessage); + case Proto.Code.MessageBodyEmpty: + throw new PayloadEmptyException((int)statusCode, requestId, statusMessage); case Proto.Code.TooManyRequests: throw new TooManyRequestsException((int)statusCode, requestId, statusMessage); case Proto.Code.RequestHeaderFieldsTooLarge: