Kong Gateway modify the source code to complete a custom error response

Kong Gateway modify the source code to complete a custom error response

#Why?

Recently, a set of gateway system based on kong is being deployed. The default error of kong and the plug-in prompt response in kong do not conform to the development unified specifications.

Tips: Response Transformer Plugins cannot meet development needs

By consulting the official documents and the official Github issue, it is found that only the source code of Kong Lua can be modified to meet the development needs.

#Version

Kong Version:5.1

#Author deployment method

  1. docker-compose
  2. Map the modified kong lua dir through volumes
...
volumes:
  - ./kong:/usr/local/share/lua/5.1/kong/  
... 

#Solve

#Gateway error prompt field

The error message field returned by default is message

{"message": "some error tips"}

Need to modify message to error

{"error": "some error tips"}

#Specific modification

file: /usr/local/share/lua/5.1/kong/tools/utils.lua line: 1105 ~ 1130

#Routing does not exist error prompt

Default Response

{
  "message": "no Route matched with those values"
}

change into

message has been changed to error in the previous step

{
  "error": "Route not found"
}

file1: /usr/local/share/lua/5.1/kong/runloop/handler.lua line: 1096

file2: /usr/local/share/lua/5.1/kong/plugins/acme/api.lua line: 78

#Conclusion

There are also some plug-in tips, which will not be discussed here. You can adjust them according to your needs.

#Support Author

coffee


Did you enjoyed the article ?
Subscribe to the newsletter 👨‍💻

Be the first to know when I write something new! If you don't like emails, you can follow me on GitHub.