简体   繁体   English

WSO2 Api Manager自定义错误消息

[英]WSO2 Api Manager Custom error messages

I want to customize the error messages sent by api manager, for instance when an access token is missing or expired. 我想自定义api管理器发送的错误消息,例如当访问令牌丢失或过期时。 I've configured _auth_failure_handler_ to return messages in json as described here , and get responses like: 我已将_auth_failure_handler_配置为按此处所述在json中返回消息,并获得如下响应:

{"fault":{"code":"900902","message":"Missing Credentials","description":"Required OAuth credentials not provided"}}

I would like to modify the message description and remove the "code" altogether. 我想修改消息描述并完全删除“代码”。 Is there a way to do this? 有没有办法做到这一点? I've tried tweaking the sequence with no luck. 我试过没有好运的调整顺序。

It is not wise advice to modify the error code. 修改错误代码不是明智的建议。 Nevertheless, yes it is possible to modify the payload. 但是,可以修改有效负载。 Use the filter mediator and Json path and identify the data and use enrich mediator to modify the payload as you want. 使用过滤器介体Json路径并标识数据,并使用扩展介体根据需要修改有效负载。

You need to target the error codes from https://docs.wso2.com/display/AM260/Error+Handling and update it to your custom JSON messages. 您需要定位来自https://docs.wso2.com/display/AM260/Error+Handling的错误代码,并将其更新为自定义JSON消息。 For auth token related errors try modify _auth_failure_handler_ as below: 对于与身份验证令牌相关的错误,请尝试修改_auth_failure_handler_,如下所示:

<sequence name="_auth_failure_handler_" xmlns="http://ws.apache.org/ns/synapse">
<property name="error_message_type" value="application/json"/>
<filter source="get-property('ERROR_CODE')" regex="405">
  <then>
      <sequence key="converter"/>
      <drop/>
  </then>
  <else>
  </else>
</filter>
<filter source="get-property('ERROR_CODE')" regex="900901">
    <then>
        <sequence key="invalidCredential"/>
        <drop/>
    </then>
    <else>
    </else>
</filter>
<filter source="get-property('ERROR_CODE')" regex="900902">
    <then>
        <sequence key="missingCredential"/>
        <drop/>
    </then>
    <else>
    </else>
</filter>
<sequence key="_cors_request_handler_"/>

For your case Missing Credential has a 900902 code , so it will match and need to define missingCredential.xml as below : 对于您的情况,Missing Credential具有900902代码,因此它将匹配并且需要按以下方式定义missingCredential.xml:

<?xml version="1.0" encoding="UTF-8"?>
<sequence xmlns="http://ws.apache.org/ns/synapse" name="missingCredential">
    <payloadFactory media-type="json">
        <format>{ "status": "Error","message": "Missing Credentials"  }</format>
    <!--Add your custom message and format here. This will be your output-->
    </payloadFactory>
    <property name="RESPONSE" value="true"/>
    <header name="To" action="remove"/>
    <property name="HTTP_SC" value="401" scope="axis2"/>
    <property name="messageType" value="application/json" scope="axis2"/>
    <send/>
</sequence>

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

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