blob: 3721bc9470d9745fd5761442e8eabe082720aac7 [file] [log] [blame]
/**
* 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 Kafka.Client.Exceptions
{
using System;
/// <summary>
/// A wrapping of an error code returned from Kafka.
/// </summary>
public class KafkaException : Exception
{
/// <summary>
/// No error occurred.
/// </summary>
public const int NoError = 0;
/// <summary>
/// The offset requested was out of range.
/// </summary>
public const int OffsetOutOfRangeCode = 1;
/// <summary>
/// The message was invalid.
/// </summary>
public const int InvalidMessageCode = 2;
/// <summary>
/// The wrong partition.
/// </summary>
public const int WrongPartitionCode = 3;
/// <summary>
/// Invalid message size.
/// </summary>
public const int InvalidRetchSizeCode = 4;
public KafkaException()
{
ErrorCode = NoError;
}
/// <summary>
/// Initializes a new instance of the KafkaException class.
/// </summary>
/// <param name="errorCode">The error code generated by a request to Kafka.</param>
public KafkaException(int errorCode) : base(GetMessage(errorCode))
{
ErrorCode = errorCode;
}
/// <summary>
/// Gets the error code that was sent from Kafka.
/// </summary>
public int ErrorCode { get; private set; }
/// <summary>
/// Gets the message for the exception based on the Kafka error code.
/// </summary>
/// <param name="errorCode">The error code from Kafka.</param>
/// <returns>A string message representation </returns>
private static string GetMessage(int errorCode)
{
if (errorCode == OffsetOutOfRangeCode)
{
return "Offset out of range";
}
else if (errorCode == InvalidMessageCode)
{
return "Invalid message";
}
else if (errorCode == WrongPartitionCode)
{
return "Wrong partition";
}
else if (errorCode == InvalidRetchSizeCode)
{
return "Invalid message size";
}
else
{
return "Unknown error";
}
}
}
}