Browse Source

增加 IApiException,用于自定义 Response 的 status 值。

master
Elivo 1 month ago
parent
commit
b82991bd6d
  1. 24
      Apewer/ForbiddenException.cs
  2. 46
      Apewer/NotFoundException.cs
  3. 7
      Apewer/RedundanceException.cs
  4. 5
      Apewer/UnauthorizedException.cs
  5. 48
      Apewer/Web/ApiProcessor.cs
  6. 16
      Apewer/Web/IApiException.cs

24
Apewer/ForbiddenException.cs

@ -0,0 +1,24 @@
using Apewer.Web;
using System;
namespace Apewer
{
/// <summary>表示被禁止访问的错误。</summary>
public sealed class ForbiddenException : Exception, IApiException
{
const string DefaultMessage = "禁止访问。";
/// <summary>表示禁止访问的状态。</summary>
public string Status { get => "Forbidden"; }
/// <summary></summary>
public ForbiddenException(string message = DefaultMessage) : base(message.IsEmpty() ? DefaultMessage : message) { }
/// <summary></summary>
public override string ToString() => $"<ForbiddenException> {Message}";
}
}

46
Apewer/NotFoundException.cs

@ -0,0 +1,46 @@
using Apewer.Web;
using System;
namespace Apewer
{
/// <summary>表示目标资源不存在的错误。</summary>
public class NotFoundException : Exception, IApiException
{
static string _default = FixMessage(null);
static string FixMessage(string message)
{
const string Preset = "Resource not found.";
if (message != null)
{
message = message.Trim();
if (!string.IsNullOrEmpty(message)) return message;
}
return Preset;
}
/// <summary>获取或设置默认消息。</summary>
public static string DefaultMessage { get => _default; set => _default = FixMessage(value); }
/// <summary>状态。</summary>
/// <value>Unauthorized</value>
public virtual string Status { get => "Not Found"; }
/// <summary>表示目标资源不存在的错误。</summary>
/// <remarks>默认消息:Operation is not authorized.</remarks>
public NotFoundException() : base(DefaultMessage) { }
/// <summary>表示目标资源不存在的错误。</summary>
/// <remarks>默认消息:Operation is not authorized.</remarks>
public NotFoundException(string message) : base(FixMessage(message)) { }
/// <summary>表示目标资源不存在的错误。</summary>
/// <remarks>默认消息:Operation is not authorized.</remarks>
public NotFoundException(string message, Exception innerException) : base(FixMessage(message), innerException) { }
}
}

7
Apewer/RedundanceException.cs

@ -1,9 +1,4 @@
using Apewer.Internals;
using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Runtime.Serialization;
using System.Text;
using System;
namespace Apewer namespace Apewer
{ {

5
Apewer/UnauthorizedException.cs

@ -1,11 +1,12 @@
using System;
using Apewer.Web;
using System;
namespace Apewer namespace Apewer
{ {
/// <summary>表示未授权的错误。</summary> /// <summary>表示未授权的错误。</summary>
/// <remarks>默认消息:Operation is not authorized.</remarks> /// <remarks>默认消息:Operation is not authorized.</remarks>
public class UnauthorizedException : Exception
public class UnauthorizedException : Exception, IApiException
{ {
static string _default = FixMessage(null); static string _default = FixMessage(null);

48
Apewer/Web/ApiProcessor.cs

@ -1,4 +1,5 @@
using Apewer.Network;
using Apewer.Internals;
using Apewer.Network;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Net; using System.Net;
@ -305,7 +306,17 @@ namespace Apewer.Web
if (action != null) if (action != null)
{ {
_context.ApiAction = action; _context.ApiAction = action;
if (action.Unparallel)
{
UnparallelLocker.InLock(GetUnparallelKey(action.MethodInfo), () =>
{
InvokeAction(action);
});
}
else
{
InvokeAction(action); InvokeAction(action);
}
return; return;
} }
} }
@ -438,24 +449,39 @@ namespace Apewer.Web
{ {
// 控制器初始化。 // 控制器初始化。
var initializer = ApiUtility.GetInitialier(controller); var initializer = ApiUtility.GetInitialier(controller);
var match = initializer == null ? true : initializer.Invoke(controller);
if (!match) return;
if (initializer != null)
{
var @continue = true;
UnparallelLocker.InLock(GetUnparallelKey(controller.GetType()), () =>
{
@continue = initializer.Invoke(controller);
});
if (!@continue) return;
}
// 此应用独立处理,不受反射控制。
if (application.Independent) return; if (application.Independent) return;
if (function != null) if (function != null)
{ {
// 调用 API,获取返回值。 // 调用 API,获取返回值。
_context.Controller = controller; _context.Controller = controller;
UnparallelLocker.InLock(GetUnparallelKey(function.Method), () =>
{
Invoke(_context, function.Method, function.Parameters); Invoke(_context, function.Method, function.Parameters);
});
} }
else else
{ {
// 未匹配到 Function,尝试 Default。 // 未匹配到 Function,尝试 Default。
var @default = ApiUtility.GetDefault(controller); var @default = ApiUtility.GetDefault(controller);
if (@default != null) if (@default != null)
{
UnparallelLocker.InLock(GetUnparallelKey(controller.GetType()), () =>
{ {
@default.Invoke(controller); @default.Invoke(controller);
return; return;
});
} }
// 没有执行任何 Function,尝试枚举。 // 没有执行任何 Function,尝试枚举。
@ -803,6 +829,22 @@ namespace Apewer.Web
#endregion #endregion
#region 不可并行
static TextLocker UnparallelLocker = new TextLocker();
static string GetUnparallelKey(MethodInfo method)
{
return $"{method.DeclaringType.Assembly.ToString()} | {method.DeclaringType.FullName} | {method.ToString()}";
}
static string GetUnparallelKey(Type type)
{
return $"{type.DeclaringType.Assembly.ToString()}";
}
#endregion
} }
} }

16
Apewer/Web/IApiException.cs

@ -0,0 +1,16 @@
namespace Apewer.Web
{
/// <summary>执行 API 时发生的异常。</summary>
public interface IApiException
{
/// <summary>异常状态。</summary>
string Status { get; }
/// <summary>错误消息。</summary>
string Message { get; }
}
}
Loading…
Cancel
Save