Skip to content
This repository has been archived by the owner on Aug 3, 2023. It is now read-only.

Commit

Permalink
Log bad request error details during dev (#2041)
Browse files Browse the repository at this point in the history
* Log bad request error details during dev

* Update src/commands/dev/edge/setup.rs

Co-authored-by: Joshua Nelson <[email protected]>

Co-authored-by: Joshua Nelson <[email protected]>
Co-authored-by: Cass <[email protected]>
  • Loading branch information
3 people authored Aug 31, 2021
1 parent d5a67c0 commit cc1766a
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 8 deletions.
23 changes: 20 additions & 3 deletions src/commands/dev/edge/setup.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::error::Error;
use std::fmt;
use std::path::Path;

use crate::deploy::DeployTarget;
Expand All @@ -9,7 +11,7 @@ use crate::terminal::message::{Message, StdOut};
use crate::upload;

use anyhow::{anyhow, Result};
use reqwest::Url;
use reqwest::{StatusCode, Url};
use serde::{Deserialize, Serialize};
use serde_json::json;

Expand Down Expand Up @@ -49,8 +51,12 @@ pub(super) fn upload(
.post(&address)
.header("cf-preview-upload-config-token", session_token)
.multipart(script_upload_form)
.send()?
.error_for_status()?;
.send()?;

if response.status() == StatusCode::BAD_REQUEST {
return Err(BadRequestError(crate::format_api_errors(response.text()?)).into());
}
let response = response.error_for_status()?;

if !to_delete.is_empty() {
if verbose {
Expand Down Expand Up @@ -189,3 +195,14 @@ struct Preview {
struct PreviewV4ApiResponse {
pub result: Preview,
}

#[derive(Debug)]
pub(crate) struct BadRequestError(pub(crate) String);

impl Error for BadRequestError {}

impl fmt::Display for BadRequestError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
self.0.fmt(f)
}
}
19 changes: 14 additions & 5 deletions src/commands/dev/edge/watch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use crate::commands::dev::edge::setup;
use crate::deploy::DeployTarget;
use crate::settings::global_user::GlobalUser;
use crate::settings::toml::Target;
use crate::terminal::message::{Message, StdOut};
use crate::watch::watch_and_build;

use anyhow::Result;
Expand Down Expand Up @@ -37,11 +38,19 @@ pub fn watch_for_changes(
// this allows the server to route subsequent requests
// to the proper script
let uploaded = setup::upload(&mut target, &deploy_target, &user, session_token, verbose);
if let Ok(token) = uploaded {
*preview_token = token;
} else {
refresh_session_channel.send(Some(()))?;
break;
match uploaded {
Ok(token) => {
*preview_token = token;
}
Err(err) => {
if let Some(err) = err.downcast_ref::<setup::BadRequestError>() {
// no need to refresh session here, bad request indicates problem with user code
StdOut::warn(&err.0);
} else {
refresh_session_channel.send(Some(()))?;
break;
}
}
}
}

Expand Down

0 comments on commit cc1766a

Please sign in to comment.