// // ErrorCode.cs // // Author: // MiNG // // Copyright (c) 2018 Alibaba Cloud // // Permission is hereby granted, free of charge, to any person obtaining a copy // of this software and associated documentation files (the "Software"), to deal // in the Software without restriction, including without limitation the rights // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell // copies of the Software, and to permit persons to whom the Software is // furnished to do so, subject to the following conditions: // // The above copyright notice and this permission notice shall be included in // all copies or substantial portions of the Software. // // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN // THE SOFTWARE. using System; using System.Collections.Generic; using System.Linq; using System.Reflection; namespace Aliyun.Api.LogService.Infrastructure.Protocol { public class ErrorCode { #region Known error codes /// /// SDK 内部错误。 /// internal static ErrorCode SdkInternalError { get; } = new ErrorCode(nameof(SdkInternalError)); /// ///没有提供必须的 Content-Length 请求头。 /// public static ErrorCode MissingContentLength { get; } = new ErrorCode(nameof(MissingContentLength)); /// ///不支持 Content-Type 指定的类型。 /// public static ErrorCode InvalidContentType { get; } = new ErrorCode(nameof(InvalidContentType)); /// ///没有为 Body 不为空的 HTTP 请求指定 Content-Type 头。 /// public static ErrorCode MissingContentType { get; } = new ErrorCode(nameof(MissingContentType)); /// ///压缩场景下没有提供必须的 x-log-bodyrawsize 请求头。 /// public static ErrorCode MissingBodyRawSize { get; } = new ErrorCode(nameof(MissingBodyRawSize)); /// ///x-log-bodyrawsize 的值无效。 /// public static ErrorCode InvalidBodyRawSize { get; } = new ErrorCode(nameof(InvalidBodyRawSize)); /// ///x-log-compresstype 指定的压缩方式不支持。 /// public static ErrorCode InvalidCompressType { get; } = new ErrorCode(nameof(InvalidCompressType)); /// ///没有提供 HTTP 标准请求头 Host。 /// public static ErrorCode MissingHost { get; } = new ErrorCode(nameof(MissingHost)); /// ///没有提供 HTTP 标准请求头 Date。 /// public static ErrorCode MissingDate { get; } = new ErrorCode(nameof(MissingDate)); /// ///Date 请求头的值不符合 RFC822 标准。 /// public static ErrorCode InvalidDateFormat { get; } = new ErrorCode(nameof(InvalidDateFormat)); /// ///没有提供 HTTP 请求头 x-log-apiversion。 /// public static ErrorCode MissingAPIVersion { get; } = new ErrorCode(nameof(MissingAPIVersion)); /// ///HTTP 请求头 x-log-apiversion 的值不支持。 /// public static ErrorCode InvalidAPIVersion { get; } = new ErrorCode(nameof(InvalidAPIVersion)); /// ///没有在 Authorization 头部提供 AccessKeyId。 /// public static ErrorCode MissAccessKeyId { get; } = new ErrorCode(nameof(MissAccessKeyId)); /// ///提供的 AccessKeyId 值未授权。 /// public static ErrorCode Unauthorized { get; } = new ErrorCode(nameof(Unauthorized)); /// ///没有提供 HTTP 请求头 x-log-signaturemethod。 /// public static ErrorCode MissingSignatureMethod { get; } = new ErrorCode(nameof(MissingSignatureMethod)); /// ///x-log-signaturemethod 头部指定的签名方法不支持。 /// public static ErrorCode InvalidSignatureMethod { get; } = new ErrorCode(nameof(InvalidSignatureMethod)); /// ///请求的发送时间超过当前服务处理时间前后 15 分钟的范围。 /// public static ErrorCode RequestTimeTooSkewed { get; } = new ErrorCode(nameof(RequestTimeTooSkewed)); /// ///日志项目(Project)不存在。 /// public static ErrorCode ProjectNotExist { get; } = new ErrorCode(nameof(ProjectNotExist)); /// ///请求的数字签名不匹配。 /// public static ErrorCode SignatureNotMatch { get; } = new ErrorCode(nameof(SignatureNotMatch)); /// ///超过写入日志限额。 /// public static ErrorCode WriteQuotaExceed { get; } = new ErrorCode(nameof(WriteQuotaExceed)); /// ///超过读取日志限额。 /// public static ErrorCode ReadQuotaExceed { get; } = new ErrorCode(nameof(ReadQuotaExceed)); /// ///服务器内部错误。 /// public static ErrorCode InternalServerError { get; } = new ErrorCode(nameof(InternalServerError)); /// ///服务器正忙,请稍后再试。 /// public static ErrorCode ServerBusy { get; } = new ErrorCode(nameof(ServerBusy)); #endregion private static readonly IDictionary KnownErrorCodes; public String Code { get; } static ErrorCode() { KnownErrorCodes = new Dictionary(typeof(ErrorCode) .GetProperties(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static) .Where(x => x.PropertyType == typeof(ErrorCode)) .Select(x => x.GetValue(null)) .Cast() .ToDictionary(x => x.Code, x => x)); } private ErrorCode(String code) { this.Code = code; } public static Boolean IsKnownErrorCode(String code) { return KnownErrorCodes.ContainsKey(code); } public Boolean IsKnownErrorCode() { return IsKnownErrorCode(this.Code); } public override Boolean Equals(Object obj) { if (ReferenceEquals(null, obj)) { return false; } if (ReferenceEquals(this, obj)) { return true; } switch (obj) { case ErrorCode other: return String.Equals(this.Code, other.Code); case String other: return String.Equals(this.Code, other); default: return false; } } public override Int32 GetHashCode() => this.Code?.GetHashCode() ?? 0; public override String ToString() => this.Code; public static implicit operator String(ErrorCode code) => code?.Code; public static implicit operator ErrorCode(String code) => code == null ? null : (KnownErrorCodes.TryGetValue(code, out var errorCode) ? errorCode : new ErrorCode(code)); public static Boolean operator ==(ErrorCode lhs, ErrorCode rhs) => lhs?.Code == rhs?.Code; public static Boolean operator !=(ErrorCode lhs, ErrorCode rhs) => lhs?.Code != rhs?.Code; } }