微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

使用 OnError 处理程序时,如何从 REST 服务返回状态代码错误?

如何解决使用 OnError 处理程序时,如何从 REST 服务返回状态代码错误?

我正在尝试使用 ColdFusion 10 设置一些 REST Web 服务,如果我在 application.cfc 中有一个 onError 处理程序,则错误状态代码不会返回给消费者。

让我们考虑一下这个 application.cfc

<cfcomponent displayname="ApplicationCFC" output="true" >
    <cfscript>
        this.name = "learnWith";
        this.applicationTimeout = createTimeSpan(1,1,0);
        this.sessionManagement = "false";
        this.restsettings.autoregister = true;
        this.restsettings.skipCFCWithError = true;
    </cfscript>

    <cffunction name="onApplicationStart" returntype="boolean" output="true">
        <cfset RestinitApplication(expandpath('/myDir'),"lw")>
        <cfreturn true>
    </cffunction>
</cfcomponent>

现在考虑服务,如下所示:

<cfcomponent rest="true" restpath="/user">
    <cffunction name="authenticate" access="remote" restpath="/login" returntype="String" httpmethod="POST"
            consumes="application/json" produces="application/json">
        <cfset requestData = getHTTPRequestData()>
        <cfset userInfo = deserializeJSON(ToString(requestData.content)) />

        <!--- cfquery to load user data ---->

        <cfif local.dataQuery.recordcount EQ 1>
            <cfset local.userVO = createObject()> <!--- create and populate userVo here --->
            <cfreturn SerializeJSON(local.userVO)>
        <cfelse>
            <!--- user not found,throw 401 error --->
            <cfthrow type="RestError" errorcode="401"  />
        </cfif>
    </cffunction>
</cfcomponent>

代码从 Postman 完美运行。如果我传入正确的凭据

{
    "userName": "user","password": "hashedPassword
}

我按预期返回了一个用户对象,响应为 200。

Correct Credentials Login

如果我传入虚假或无效的凭据:

{
    "userName": "fakeuser","password": "fakePassword
}

我收到 401 错误

Failed Authentication

太完美了。但是,我想对 CFC 使用全局错误处理程序,既用于基于非 REST 服务的代码,也用于记录可能的 REST 服务错误。如果我将其添加application.cfc

    <cffunction name="onError" returnType="void" output="true">
        <cfargument name="exception" required="true">
        <cfargument name="eventname" type="string" required="true">
        <!--- error logging here --->
        <cfthrow type="RestError" errorcode="401" />
        <!--- or 
            <cfthrow type="#arguments.exception.Cause.Cause.type#" 
                     errorcode="#arguments.exception.Cause.Cause.code#">--->

    </cffunction>

ColdFusion 返回 500 内部服务器错误消息。

500 Internal Server Error

我在 CFC 方法和 onError 处理程序中都试验了 cfheader:

<cfheader statusCode = "401" statusText = "RestError">

它会返回一个没有正文的 200 而不是 401 状态:

200 error

我已经尝试了各种迭代,但不知所措。我可以使用来自 ColdFusion 休息服务的状态代码,同时也有一个 onError 处理程序吗?如果是这样,如何?

解决方法

我能够做我想做的事。我可以使用 restSetResponse 创建我需要的响应,而不是使用 cfthrow 来创建错误。

在 cffunction 内部,首先确保将返回类型设置为 void。

然后,您希望手动创建响应对象并使用 restSetResponse 将其发送回,而不是返回对象或使用 cfthrow。

这解决了我的问题:

<cfcomponent rest="true" restpath="/user">
    <cffunction name="authenticate" access="remote" restpath="/login" returntype="void" httpmethod="POST"
            consumes="application/json" produces="application/json">
        <cfset requestData = getHTTPRequestData()>
        <cfset userInfo = deserializeJSON(ToString(requestData.content)) />

        <!--- cfquery to load user data ---->

        <cfset local.result = structNew()/>

        <cfif local.dataQuery.recordcount EQ 1>
            <cfset local.userVO = createObject()> <!--- create and populate userVo here --->

            <cfset local.result.status = 200 />
            <cfset local.result.content = SerializeJSON(local.userVO) />
        <cfelse>
            <cfset local.result.status = 401 />
            <cfset local.result.content = SerializeJSON({message: 'User Not Authorized'}) />
        </cfif>

        <cfset restSetResponse(local.result) />
    </cffunction>
</cfcomponent>

一旦我这样做了,我就能够控制返回给我的消费者的响应,而无需触发 onError 中的 Application.cfc 方法。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。