简体   繁体   English

使用不同数据结构处理Ajax错误的方法

[英]Approach for ajax error handling with different data structures

I want to use two or three modules to show errors but I have many different error data structures as responses from ajax. 我想使用两个或三个模块来显示错误,但是我有许多不同的错误数据结构作为ajax的响应。 Repetitious if/else chains for each ajax error callback or each error display module should be avoided. 应该避免每个ajax错误回调或每个错误显示模块的重复if / else链。 Here are some examples of error responses I receive in terms of properties to show to the user: 以下是一些根据属性显示给用户的错误响应示例:

500 Server Error: {
    ...
    Message: "An error has occurred.",
    ...
}

401 Login Error: {
    ...
    Reason: "Invalid login credentials.",
    ...
}

400 Bad Request on Put: {
    Errors: [
        {
            PropertyName: "EmailAddress",
            ErrorName: "Invalid email address."
        }
    ]
}

Currently I'm considering taking each error response through $httpProvider.interceptors in angular (similar to ajaxSetup in jQuery?) and forcing consistent property names onto each one. 目前,我正在考虑通过$httpProvider.interceptors以角度(类似于jQuery中的ajaxSetup?)获取每个错误响应,并将一致的属性名称强制设置为每个名称。 I don't know if this is bad design, but it would certainly make things easier down the line. 我不知道这是不是一个糟糕的设计,但这肯定会使事情变得更容易。

One of the several ways you could solve this is to use two concepts: 解决此问题的几种方法之一是使用两个概念:

Single Responsability principle: 单一责任原则:

Obviously you can mash everything up and get something working. 显然,您可以将所有内容混搭起来并使其正常工作。 In my comment I suggested that the Adaptor pattern but when making this an answer I realised that wan't good enough. 在我的评论中,我建议使用适配器模式,但是当回答这个问题时,我意识到它不够好。 If so, your application or would still have a part of it's code responsible for choosing the adaptor, so we can encapsulate that into a factory that provides you the proper adaptor: 如果是这样,您的应用程序或它的一部分代码仍将负责选择适配器,因此我们可以将其封装到为您提供适当适配器的工厂中:

So we would have three main components: 因此,我们将具有三个主要组成部分:

  • Application: responsible for the app logic, gets the error object and passes it along to... 应用程序:负责应用程序逻辑,获取错误对象并将其传递给...
  • Factory: responsible for analysing the error object and determining which adaptor suites the needs 工厂:负责分析错误对象并确定哪个适配器满足需求
  • Adaptor(s): responsible for dealing with the error and processing it in a uniform way independently of it's internal format. 适配器:负责处理错误并以统一的方式处理错误,而与内部格式无关。

In JavaScript this could be something like: 在JavaScript中,可能类似于:

function serviceResponse(error, response) {

  // assuming error could be all different shapes and sizes
  if (error) {

    adaptorFactory
      .getAdaptor(error)
      .process(error)    
  }

  // your valid response logic

} 

Future changes to accommodate new error formats 未来的变化以适应新的错误格式

If you have a new error, you'll have to do two changes, create a new adaptor object and a new if/else in the factory. 如果有新错误,则必须做两个更改,在工厂中创建一个新的适配器对象和一个新的if/else But the beauty of it is that you will not touch your application logic at all. 但是它的优点是您完全不会碰到您的应用程序逻辑 That serviceResponse method stays exactly as it is serviceResponse方法保持原样

Again, there are plenty of good solutions, this is just one approach. 同样,有很多好的解决方案,这只是一种方法。 You can develop the adaptor and factory as you wish, as long as the key concepts are kept. 只要保留了关键概念,就可以根据需要开发适配器和工厂。 My examples were for illustration purposes only. 我的示例仅用于说明目的。

Hope this helps. 希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM