是否可以重写CodeIgniter框架的默认错误处理程序以将其输出为json? [英] Is it possible to override default error handler of CodeIgniter framework to output it as json?

查看:64
本文介绍了是否可以重写CodeIgniter框架的默认错误处理程序以将其输出为json?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

CodeIgniter的show_error函数可以处理错误,有某种方法可以覆盖它以将其信息作为json对象输出?

解决方案

如果您希望返回复杂的错误对象(例如数组)(虽然可以,但是您必须在客户端上解析json),也许此解决方案并不完全有用,但是我们可以:



只需使用HTTP



为什么?好吧,您可以向其传递第二个参数,该参数恰好是HTTP响应代码,该参数很酷,因为它允许您使应用程序具有HTTP感知,并且对客户端Ajax请求非常有效。 / p>

您要做的是首先定义后端可能发生的错误类型,其中包含HTTP错误代码列表此处



HTTP错误代码



您很有可能会在200、400和500范围内使用错误代码。实际上,当您在网络浏览器上访问服务器时,通常会收到200 http响应代码,这意味着一切正常。



您是否看到过这些内部服务器错误消息?好吧,它们是500个http响应代码。确切的说,这是服务器发出的错误。哪一个?这取决于您如何对它们进行分类,在500范围内会出现一系列错误,但是如果您不想为此烦恼,只需使用500通用错误代码即可。



另一个范围是400。通常是用户的错误,例如,如果您访问服务器内部的url,但该URL不存在,则将找不到著名的404,400是一个通用错误代码,表示客户端(在这种情况下,浏览器)请求了一些内容,但请求无效,特别是在404的情况下,找不到您请求的资源,这是客户端错误,因为您应该知道哪些资源可用



如何在codeigniter中实现



这非常简单其实。如果您在文档中看到 show_error()参考,则说明该方法收到了第一个参数作为错误消息,第二个(可选)接收错误代码。哪个错误代码?我们之前已经讨论过HTTP代码,所以:

  show_error('Howdy,这是我的调试消息',500); 

将向客户端发送500 HTTP响应代码,包括您的消息。



如何捕获AJAX



考虑到您正在使用jQuery,这通常是您要做的事情:

  $。ajax({
type:'POST',
url:example.com/resource,
data:$(#some-form)。serialize(),
dataType:'json',
success:function(data,textStatus,req){
/ /使用数据执行某些操作,该数据是从PHP
}返回的json对象,
错误:function(req,textStatus,errorThrown){
//这将在您发送其他内容时发生从200 OK HTTP
alert('哎呀,发生了什么事:'+ textStatus +''+ errorThrown);
}
});

如果您使用的是任何其他工具包,甚至直接使用DOM对象,由于它们是只需 XMLHttpRequest对象,您的工具包就有可能回调HTTP错误响应或成功响应。



我为什么要在意?



由于遵循标准,因此易于调试,因此委托可以使用show_error()助手,这是有原因的,最重要的是因为所有酷孩子都在使用它



很酷,但是等等,我没有自定义错误消息!



是的,因为当您在jquery的错误回调中捕获请求时,您得到的是通用错误描述以及分别为内部服务器错误和500之类的代码,但是,您仍然得到了漂亮的html响应您的自定义调试消息,要查看该消息,只需使用某种针对Firefox或Chrome的开发者工具即可。例如,如果您使用google chrome,则可以打开开发人员工具:



转到网络标签,您会看到HTTP请求,单击其名称





您将看到详细信息和带有常规CI模板的自定义错误消息,这是在请求内随消息返回的html





最后,如果您想进一步挖掘和调试从php / web服务器发送到客户端的确切信息转到标头选项





免责声明: 屏幕截图并非来自生产服务器:)


The show_error function of CodeIgniter handle the errors, there is some way to override it to output their info as a json object?

解决方案

Well maybe this solution isn't entirely useful if you are looking to return complex error objects like arrays (although you could, but you'd have to parse json on the client) but here we go:

Just use HTTP

Why? Well, you can pass a second parameter to it, which happens to be the HTTP response code, which happens to be cool, cause it allows you to make your application HTTP-aware and that works amazingly good with client-side ajax requests.

What you want to do is first define what kind of errors can happen on the backend, there is a list of HTTP error codes here.

HTTP error codes

Most likely you'll use the error codes in the 200, 400 and 500 ranges. Actually when you hit a server on your web browser it would normally receive a 200 http response code which mean everything went fine.

Have you seen those "Internal server eror" messages? Well they are 500 http response codes. And that means exactly that, it was an error from the server. Which one? It depends how you categorize them, there's a set of errors on the 500 range, but if you don't want to hassle with that just use a 500 generic error code response.

The other range is 400. Those are usually error from users, for example if you go to a url inside a server and it does not exist you'd get the famous 404 not found, 400 is a generic error code meaning that the client (in this case, the browser) requested something but the request was invalid, in the case of a 404 specifically, that the resource you requested was not found, it is a client error because you are supposed to know which resources are available on the server.

How to do it in codeigniter

It is extremely simple actually. If you see the show_error() reference on the documentation it states that the method receives a first parameter as the error message, and a second, optional, that receives an error code. Which error code? The HTTP codes we talked about before, so:

show_error('Howdy, this is my debug message', 500);

Would send a 500 HTTP response code to the client, including your message.

How to catch in AJAX

Considering you are using jQuery this is what you would normally do:

$.ajax({
    type: 'POST',
    url : example.com/resource,
    data: $("#some-form").serialize(),
    dataType: 'json',
    success : function(data, textStatus, req) {
        //do something with data which is a json object returned from PHP
    },
    error: function(req, textStatus, errorThrown) {
        //this is going to happen when you send something different from a 200 OK HTTP
        alert('Ooops, something happened: ' + textStatus + ' ' +errorThrown);
    }
});

If you were using any other toolkit or even the DOM object directly you can still catch those since they are simply XMLHttpRequest objects and chances are your toolkit has a callback for either an HTTP error response or a success response.

Why would I care?

Because it follows standards, its easier to debug, you delegate that work to the show_error() helper which is there for a reason and most importantly because all the cool kids are using it.

Cool, but wait, I don't see my custom error message nowhere!

That is right, because when you catch the request in the error callback of jquery what you get is the generic error description and the code like "Internal server error" and 500 respectively, however, you still got a pretty html response with your custom debug message, to see it just use some sort of developer tool for firefox or chrome. For example, if you use google chrome you can open the developer tools:

Go to network tab and you'll see the HTTP request, click on its name

You'll see the details and your custom error message with the usual CI template, this was the html returned with your message inside the request

Finally if you want to dig further and debug exactly what was sent from php/web server to the client go to the headers option

Disclaimer: Screenshots were not taken from a production server :)

这篇关于是否可以重写CodeIgniter框架的默认错误处理程序以将其输出为json?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆