public class ECOrderException : Exception
{
//第一种类型: throw new ECOrderException { ErrorCode = "X1008-06", TransMessage = "SNs格式错误" };
public ECOrderException() : base()
{
}
public virtual string ErrorCode { get; set; }
public virtual string TransMessage { get; set; }
//第二种类型:throw new ECOrderException("CO,WHSE 等栏位不能为空");
//public ECOrderException(string Message): base(Message)
//{
// TransMessage = Message;
//}
}
public class VWSException : Exception
{
public VWSException() : base()
{
} public string ErrorCode { get; set; }
public string TransMessage { set; get; } public VWSException(string Message) : base(Message)
{
TransMessage = Message;
}
}  throw new VWSException { ErrorCode = "03", TransMessage = $"SO Confirm Failed,{ex.Message}" };

上面,有两个虚拟字段Error_Code和TransMessage

使用的时候,可以这样使用:

   throw new ECOrderException { ErrorCode = "X1008-01", TransMessage = "交易代码验证失败" };
 var obj = new JObject(
new JProperty("head",
new JObject(
new JProperty("transMessage", transMessage),
new JProperty("errorCode", ErrorCode)
)),
new JProperty("body",
new JObject())
); var result = new ContentResult()
{
Content = Newtonsoft.Json.JsonConvert.SerializeObject(obj),
ContentType = "application/json"
};

然后这样就可以返回一个Json的格式信息给前端。

⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐

ASP.NET Web API 异常处理 HttpResponseException 以及Angularjs获取异常信息并提示

一、HttpResponseException

  如果一个Web API控制器抛出一个未捕捉异常,默认地,大多数异常都会被转化成一个带有状态码“500 – 内部服务器错误”的HTTP响应。HttpResponseException(HTTP响应异常)类型会返回你在异常构造器中指定的任何HTTP状态码。例如,在以下方法中,如果id参数非法,会返回“404 — 未找到”。

public Product GetProduct(int id)
{
Product item = repository.Get(id);
if (item == null)
{
     //指定响应状态码
throw new HttpResponseException(HttpStatusCode.NotFound);
}
return item;
}

为了对响应进行更多控制,你也可以构造整个响应消息HttpResponseMessage,并用HttpResponseException来包含它:

public Product GetProduct(int id)
{
Product item = repository.Get(id);
if (item == null)
{
var resp = new HttpResponseMessage(HttpStatusCode.NotFound)
{
Content = new StringContent(string.Format("No product with ID = {0}", id)),
ReasonPhrase = "Product ID Not Found"
}
     //包含一个HttpResponseMessage
throw new HttpResponseException(resp);
}
return item;
}
if (order == null)
{
throw new HttpResponseException(Request.CreateResponse(HttpStatusCode.NotFound));
}
 

继承Exception⭐⭐的更多相关文章

  1. Java继承Exception自定义异常类教程以及Javaweb中用Filter拦截并处理异常

    转载请注明原文地址:http://www.cnblogs.com/ygj0930/p/6403033.html 在项目中的应用见: https://github.com/ygj0930/CoupleS ...

  2. 类ExampleA继承Exception,类ExampleB继承ExampleA。 有如下代码片断:

    try { throw new ExampleB("b") } catch(ExampleA e){ System.out.println("ExampleA" ...

  3. 类Exception_A继承Exception,类Exception_B继承Exception_A,请问执行此段代码的输出是什么?

    @Test public void Test_Exception() { try { throw new ExceptionB("A"); } catch (ExceptionA ...

  4. Java基础知识系列——Exception

    异常在编程中使用频率非常非常的高,在Java中异常的基类是Exception. 下面就介绍一下Java中的异常: 1.结构 try{ //捕获try里的异常 }catch( Exception e){ ...

  5. Thinking in java中关于Exception的一道面试题.

    今天看到Thinking in Java中一个关于Exception的例子:最后看到有一篇总结的比较好的文章, 这里拿来记录下, 文章地址是:http://blog.csdn.net/salerzha ...

  6. 05_Java异常(Exception)

    1. 异常的概念 1.1什么是异常 异常指的是程序运行时出现的不正常情况. 1.2异常的层次 Java的异常类是处理运行时的特殊类,每一种异常对应一种特定的运行错误.所有Java异常类都是系统类库中E ...

  7. Java-异常Throwable,Exception,Error

      异常指不期而至的各种状况,如:文件找不到.网络连接失败.非法参数等. 异常是一个事件,它发生在程序运行期间,干扰了正常的指令流程. Java通过API中Throwable类的众多子类描述各种不同的 ...

  8. Java基础(55):Exception类详解(转)

    Java中的异常 Exception java.lang.Exception类是Java中所有异常的直接或间接父类.即Exception类是所有异常的根类. 比如程序: public class Ex ...

  9. Java:Exception

    异常: 就是程序在运行时出现不正常的情况. 异常的由来:问题也是现实生活中一个具体的事物,也可以通过java的类的形式进行描述,并封装成对象.其实就是Java对不正常情况进行描述后的对象的体现. 两种 ...

随机推荐

  1. maven环境隔离

    pom <build>节点下增加节点 <resources> <resource> <directory> src/main/resources.${d ...

  2. UVa 1627 - Team them up!——[0-1背包]

    Your task is to divide a number of persons into two teams, in such a way, that: everyone belongs to ...

  3. CSS选择器权重计算规则

    从CSS代码存放位置看权重优先级:内嵌样式 > 内部样式表 > 外联样式表.其实这个基本可以忽视之,大部分情况下CSS代码都是使用外联样式表. 从样式选择器看权重优先级:important ...

  4. ES6必须 知道的小知识

    1.函数的默认参数 一般 我们给函数设置默认参数的时候  会在函数里用 || 运算符 比如 function show(width,height ....){ var height = height ...

  5. java.lang.IllegalArgumentException: attempt to create saveOrUpdate event with null entity

    今天想把ssh整合的代码跑起来,控制台就一直在报错,搞了半天!!! Hibernate: select computer0_.computerId as computer1_0_, computer0 ...

  6. asp.net core 3.0 JObject The collection type 'Newtonsoft.Json.Linq.JObject' is not supported

    在asp.net core 3.0 中,如果直接在Controller中返回 Jobject 类型,会抛出如下错误: The collection type 'Newtonsoft.Json.Linq ...

  7. 关于MySQL中查询大数据量的情况下分页limit的性能优化

    https://blog.csdn.net/weixin_37848710/article/details/80772725

  8. Vsual Studio 2010可用的sqlite驱动程序(实体数据模型使用)

    背景 昨天一个旧的项目(.net framework 4 + EF4 +sqlite + edmx db first),数据库结构有变更,要更新实体edmx模型 先是到官网下载最新的驱动,结果不能更新 ...

  9. slim的中间件

    slim中间件的作用简单来说就是过滤数据,request过来的数据要经过中间件才能到达内部,然后内部数据要到达外部的时候,也要经过中间件,正常通过才能到达外部

  10. 第二阶段:2.商业需求分析及BRD:5.商业需求文档1

    三大文档 FSD一般包含在PRD 1.BRD一般是去向决策层汇报 2.产品介绍的各项是可选的 不是必备的 产品线路图就是roodmap.团队一般是偏技术的团队. BRD案例. 痛点.定性的描述.不会非 ...