error.rs placeholder
Learn why and how to create a placeholder error.rs file in a conventional Axum project structure, with multiple examples and beginner-friendly explanations.
When you build a real web application with Axum, you will eventually need a consistent way to handle errors across all your routes and handlers. That work belongs in a dedicated module. But before you write any of that logic, you need to create the file that will hold it. This page explains what an error.rs placeholder is, why it exists, where it goes, and exactly how to set it up in your project.
Why Create an Empty error.rs File?
Imagine you are building a house. You would not wait until the very end to decide where the electrical panel goes. You would mark the location on the blueprints, run the conduit, and leave a blank space ready for the wires when the time comes. An error.rs placeholder does the same thing for your code. It is a file that exists from the beginning, sitting in the correct folder, but containing almost nothing.
In a conventional Axum project layout, each responsibility gets its own file. You already have a file for shared application state. Next you add a file for error handling. Even if you do not need custom errors yet, creating the file now accomplishes three things:
- It fixes the location. Everyone on the team knows that error-related code belongs in
src/error.rs. No one has to guess where to put a new error type six months from now. - It prevents a future refactor. If you wait until you have ten handlers and then decide to extract error handling, you will need to move code around. Starting with the placeholder means the structure is already correct.
- It documents intent. A placeholder with a small comment like
// Error types and conversion logic will live heretells future readers exactly what the file is for, even before it has any real content.
Placeholders Are Intentional:
An empty file is not a mistake. It is a deliberate part of the project skeleton. Many mature codebases contain placeholder files that get filled in as features are built.
Where the File Belongs
In the conventional layout, your src directory looks something like this:
src/
├── main.rs
├── lib.rs
├── routes/
│ └── mod.rs
├── handlers/
│ └── mod.rs
├── models/
│ └── mod.rs
├── state.rs
└── error.rs <-- you are here
state.rs sits right next to it, directly inside src/. The conventional Axum layout keeps top-level shared modules like state and error at the root of the crate, while feature‑specific code goes into subdirectories like routes, handlers, and models.
If you are using an older or simpler layout where everything is still flat, error.rs would still go in the same directory as main.rs. The important point is that it lives at the same level as other shared modules, not buried inside a feature folder.
Creating the Placeholder
You can create the file with any text editor, or from the command line:
touch src/error.rs
What you put inside depends on how much structure you want to establish right away. There are three common approaches, each useful in a different situation.
Option 1: Completely Empty File
// src/error.rs
That's it. An empty file is a valid placeholder. Rust will not complain about an empty file as long as the module is later declared in main.rs or lib.rs. This option is best when you truly do not know yet what your error handling will look like. It says, "I will define this later," without pretending to know the shape.
Option 2: Comment Only
// src/error.rs
// Application-specific error types and conversion to HTTP responses will go here.
Adding a single comment makes the file's purpose obvious to anyone browsing the repository. The comment does not affect compilation; it is purely documentation. This is the most common placeholder style because it costs nothing but removes ambiguity.
Option 3: Stub Type Alias
Sometimes you want to start using the error module from other files immediately, even if the real implementation is not ready. In that case, you can define a placeholder type alias that handlers can reference.
// src/error.rs
// A temporary placeholder error type.
// Replace this with a real custom error enum later.
pub type Error = Box<dyn std::error::Error + Send + Sync>;
Now other modules can write use crate::error::Error; and their code will compile. Box<dyn std::error::Error + Send + Sync> is a generic "any error" type, acceptable as a placeholder. When you implement proper error handling, you replace this alias with a concrete type.
Do Not Leave the Stub in Production:
The Box<dyn Error> alias is convenient for early development, but it gives you almost no information about what went wrong. Do not ship a real application with this placeholder still in place. Replace it before your first release.
Which option should you choose?
- If you are following the conventional layout strictly and will not write any error handling until a later phase, use Option 1 or Option 2.
- If you already have a handler that returns a
Resultand you want a temporary error type to satisfy the compiler, use Option 3.
For most readers of this page, Option 2 (comment only) is the best balance of structure and simplicity.
What Will Eventually Live in error.rs?
A word of caution: this page is only about the placeholder. The real content of error.rs is a large topic of its own. However, a high-level picture helps you understand why the file exists.
In Axum, a handler can return a Result<T, E> where E is an error type. If E implements the IntoResponse trait, Axum can convert an Err variant into an HTTP response automatically. A custom error module typically contains:
- An
enumthat lists all the possible errors your application can produce (for example,NotFound,DatabaseError,ValidationError). - An implementation of
IntoResponsefor that enum, mapping each variant to an appropriate HTTP status code and message. - Helper functions to convert low-level errors (like
sqlx::Errororserde_json::Error) into your custom error type.
That work belongs in error.rs. But you do not write it now. You only create the container.
One File, One Job:
The placeholder is the first step in the "separation of concerns" principle. error.rs will own everything related to error handling. No other file should define its own ad-hoc error mappings; they should reuse the types from this module.
Declaring the Module
Creating error.rs is not enough for Rust to know about it. Rust only compiles files that are declared as modules. Since this page assumes you have already split main.rs and lib.rs, the declaration belongs in lib.rs, where all other modules are declared.
If you look at src/lib.rs, you should see something like this:
// src/lib.rs
pub mod routes;
pub mod handlers;
pub mod models;
pub mod state;
You will need to add a line for error:
// src/lib.rs
pub mod routes;
pub mod handlers;
pub mod models;
pub mod state;
pub mod error;
If you do not add this declaration, the file error.rs will be completely ignored by the compiler, and any attempt to use crate::error::... will fail with an unresolved import error, E0432, saying the compiler could not find error in the crate root.
Forgotten Module Declaration Breaks Everything:
A common beginner mistake is to create the placeholder file but forget to declare the module. Then, when you later try to use crate::error, the compiler reports an unresolved import error, and you spend time wondering why the file exists but cannot be found. Always add the pub mod error; line immediately after creating the file.
What You Should See After This Step
After creating src/error.rs (with any of the three placeholder options) and adding the pub mod error; line to lib.rs, your project should still compile without errors:
cargo check
The output should be clean, with no warnings about unused code. (A pub type alias in a library crate never triggers a dead-code warning, even if nothing references it yet - pub items are considered part of the crate's public API.)
Placeholder Is Working:
If cargo check succeeds and your file listing shows src/error.rs, the placeholder is correctly set up. You now have a dedicated home for future error handling code.
Common Beginner Mistakes
- Creating the file in the wrong directory. The placeholder belongs in
src/, not insrc/routes/orsrc/handlers/. Error handling is a cross-cutting concern, a job that touches every part of the application rather than one feature, so its module lives at the crate root. - Forgetting the
pub mod error;declaration. This is so common that it deserves repeating. The file alone does nothing. - Writing real error handling code now. The whole point of this page is to create an empty or minimal placeholder. Resist the urge to define a full error enum, implement
IntoResponse, or handle specific failures. That comes later. - Using the stub
Box<dyn Error>and thinking it is good enough for production. It is not. It is a temporary measure to let other code compile while you work on other parts of the project.
Summary
You now have an error.rs file in the correct location, ready to receive error handling logic when you reach that stage. This small act of creating a placeholder keeps your project structure consistent and predictable. It also forces you to think about where errors will be handled, even before you write a single handler that can fail.
The key decision is which placeholder style to use: empty file, comment, or stub type alias. For most projects in their early phases, a file with a one-line comment is perfect. It communicates intent without locking you into a design.
With error.rs in place, your project now has the foundational structure for shared state and shared errors. The remaining pieces - module declarations, re-exports, and more advanced layout patterns - build on this skeleton.