Bad WPI Web Design

This is an example of a poor design layout. Even though WPI has managed to create a refined version of this concept, it is still unknown why this version exists. For starters, this page uses an…

Smartphone

独家优惠奖金 100% 高达 1 BTC + 180 免费旋转




Error Handling in Rest APIs using the Axum Framework and Custom Error Model

This article discusses error handling strategies while working with the Axum framework and writing REST APIs in Rust. While Axum is a very well written framework and has nice paradigms, it is our responsibility as programmers to handle errors gracefully and meaningfully within the framework.

We’ll look at two different approaches and build upon these to construct an error handling model that can be used by everyone to share and write high quality handlers with error handling taken care of.

Axum provides a very simple way of error handling. A principle to understand is that the application cannot crash if an error occurs. All errors must be Infallible .

Simply put, all errors are responses that can be rendered over HTTP.

Just like we would represent a JSON Object as a Response, we can model our errors to be objects that can be served to our clients — with the right response codes. We’ll look two different ways to build this in this post. A simple Status response with String errors. For the sake of simplicity, we will start with text responses here which can be rendered by AXUM’s built in StatusCode error renderer.

Let’s create a new application for this demo.

Add the following dependencies to start writing our APIs.

We’ll add a simple handler that expects a query param and returns the same within our response. It’s like a hello world with an addition of query param value. Below is our app definition that renders this endpoint at /hello

With that setup, we can run our server ( cargo run ) , and test it on our local machine.

Let’s try making a call to our API by using the following endpoint.

And we would see a response with our name in it.

Now, try calling this API without passing the query param. We see that we have the following response now.

Add a comment

Related posts:

Bibit Zaitun Obr Tanaman Black Mission Paling Laris Magetan Terlaris

bibit zaitun obr tanaman black mission paling laris #. Hanya menjual produk dengan kualitas terbaik. #. Pengiriman lebih cepat (order hari ini dikirim hari itu juga). #. Langsung dari suplier. {bukan…

India Went Deep

I sat next to a man for ten days and never said hi, learned his name, or communicated in any way other than the occasional bow of the head as to indicate an apology if one of us were to bump into the…

How to develop a major donor programme

A well-functioning major donor programme can provide a powerful return on investment for charities of every size. Depending on what constitutes “major” for your charity, cultivation and stewardship…