Commit b7fa4677 authored by bksp's avatar bksp
Browse files

better error handling

parent 7155c1cc
#![feature(proc_macro_hygiene, decl_macro)]
use std::str::FromStr;
#[macro_use] extern crate rocket;
use rocket::State;
use rocket::http::Status;
use rocket_contrib::json::Json;
use trust_dns_client::client::{Client, SyncClient};
use trust_dns_client::tcp::TcpClientConnection;
use trust_dns_client::op::DnsResponse;
use trust_dns_client::op::{DnsResponse, ResponseCode};
use trust_dns_client::rr::{DNSClass, Name, Record, RecordType};
mod types;
mod models;
mod config;
use models::errors::ErrorResponse;
fn zone_records(client: State<SyncClient<TcpClientConnection>>, zone: String) -> Json<Vec<types::dns::Record>> {
fn zone_records(client: State<SyncClient<TcpClientConnection>>, zone: String) -> Result<Json<Vec<models::dns::Record>>, ErrorResponse<()>> {
// TODO: Implement FromParam for Name
let name = Name::from_str(&zone).unwrap();
let name = Name::from_utf8(&zone).unwrap();
let response: DnsResponse = client.query(&name, DNSClass::IN, RecordType::AXFR).unwrap();
if response.response_code() != ResponseCode::NoError {
return ErrorResponse::new(
format!("zone {} could not be found", name.to_utf8())
let answers: &[Record] = response.answers();
let mut records: Vec<_> = answers.to_vec().into_iter()
.map(|record| types::dns::Record::from(record))
.map(|record| models::dns::Record::from(record))
.filter(|record| match record.rdata {
types::dns::RData::NULL { .. } | types::dns::RData::DNSSEC(_) => false,
models::dns::RData::NULL { .. } | models::dns::RData::DNSSEC(_) => false,
_ => true,
// AXFR response ends with SOA, we remove it so it is not doubled in the response.
fn main() {
use serde::Serialize;
use rocket::http::Status;
use rocket::request::Request;
use rocket::response::{self, Response, Responder};
use rocket_contrib::json::Json;
#[derive(Serialize, Debug)]
pub struct ErrorResponse<T> {
#[serde(with = "StatusDef")]
pub status: Status,
pub message: String,
#[serde(skip_serializing_if = "Option::is_none")]
pub details: Option<T>
#[serde(remote = "Status")]
struct StatusDef {
code: u16,
#[serde(rename = "status")]
reason: &'static str,
impl<T> ErrorResponse<T> {
pub fn new(status: Status, message: String) -> ErrorResponse<T> {
ErrorResponse {
details: None,
pub fn with_details(self, details: T) -> ErrorResponse<T> {
ErrorResponse {
details: Some(details),
pub fn err<R>(self) -> Result<R, ErrorResponse<T>> {
impl<'r, T: Serialize> Responder<'r> for ErrorResponse<T> {
fn respond_to(self, req: &Request) -> response::Result<'r> {
let status = self.status;
pub mod dns;
pub mod errors;
pub mod trust_dns_types {
pub use trust_dns_client::rr::rdata::{
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment