Skip to content

Commit

Permalink
Handle request errors using a new pane
Browse files Browse the repository at this point in the history
  • Loading branch information
danirod committed Feb 7, 2025
1 parent 1d06ac4 commit f8d3202
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 2 deletions.
14 changes: 14 additions & 0 deletions data/ui/response_panel.blp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,20 @@ template $CarteroResponsePanel: Adw.Bin {
};
}

StackPage {
name: "error";

child: Adw.StatusPage error_page {
icon-name: "network-error-symbolic";
title: _("Request error");

child: Gtk.Label error_extra {
label: "PRUEBA";
selectable: true;
};
};
}

StackPage {
name: "response";

Expand Down
5 changes: 5 additions & 0 deletions src/widgets/endpoint_pane.rs
Original file line number Diff line number Diff line change
Expand Up @@ -471,6 +471,11 @@ impl EndpointPane {
imp.extract_endpoint()
}

pub fn show_error(&self, error: CarteroError) {
let imp = self.imp();
imp.response.show_error(error);
}

/// Executes an HTTP request based on the current contents of the pane.
///
/// TODO: Should actually the EndpointPane do the requests? This method
Expand Down
22 changes: 21 additions & 1 deletion src/widgets/response_panel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,19 @@ use serde_json::Value;
use sourceview5::prelude::BufferExt;
use sourceview5::LanguageManager;

use crate::client::RequestError;
use crate::entities::ResponseData;
use crate::error::CarteroError;
use crate::objects::KeyValueItem;
use glib::subclass::types::ObjectSubclassIsExt;

mod imp {
use std::cell::RefCell;

use crate::error::CarteroError;
use crate::widgets::{CodeView, ResponseHeaders, SearchBox};
use adw::prelude::*;
use adw::subclass::bin::BinImpl;
use adw::{prelude::*, StatusPage};
use glib::object::Cast;
use glib::subclass::InitializingObject;
use glib::Properties;
Expand All @@ -53,6 +56,10 @@ mod imp {
#[template_child]
stack: TemplateChild<gtk::Stack>,
#[template_child]
error_page: TemplateChild<StatusPage>,
#[template_child]
error_extra: TemplateChild<Label>,
#[template_child]
pub response_headers: TemplateChild<ResponseHeaders>,
#[template_child]
pub response_body: TemplateChild<CodeView>,
Expand Down Expand Up @@ -147,6 +154,14 @@ mod imp {
self.search_revealer.set_visible(false);
self.response_body.grab_focus();
}

pub(super) fn show_error(&self, error: CarteroError) {
// TODO: Internationalize
let message = error.to_string();
self.error_page.set_description(Some(&message));
self.error_extra.set_visible(false);
self.stack.set_visible_child_name("error");
}
}
}

Expand Down Expand Up @@ -185,6 +200,11 @@ impl ResponsePanel {
Object::builder().build()
}

pub fn show_error(&self, error: CarteroError) {
let imp = self.imp();
imp.show_error(error);
}

pub fn start_request(&self) {
let imp = self.imp();

Expand Down
2 changes: 1 addition & 1 deletion src/win.rs
Original file line number Diff line number Diff line change
Expand Up @@ -476,7 +476,7 @@ mod imp {
if let Some(pane) = window.current_pane().and_then(|e| e.endpoint())
{
if let Err(e) = pane.perform_request().await {
window.toast_error(e);
pane.show_error(e);
}
}
}
Expand Down

0 comments on commit f8d3202

Please sign in to comment.