Skip to content

Commit

Permalink
Revert "improvement(error-handling): main files (#2288)"
Browse files Browse the repository at this point in the history
This reverts commit 704731e.
  • Loading branch information
shamardy committed Jan 30, 2025
1 parent a3b53bf commit 908686c
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 42 deletions.
5 changes: 2 additions & 3 deletions .github/workflows/pr-lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ jobs:
types: |
feat
fix
improvement
chore
docs
deps
Expand All @@ -39,9 +38,9 @@ jobs:
TITLE: ${{ github.event.pull_request.title }}
run: |
title_length=${#TITLE}
if [ $title_length -gt 85 ]
if [ $title_length -gt 72 ]
then
echo "PR title is too long (greater than 85 characters)"
echo "PR title is too long (greater than 72 characters)"
exit 1
fi
Expand Down
39 changes: 7 additions & 32 deletions mm2src/common/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -160,14 +160,14 @@ use std::convert::TryInto;
use std::fmt::Write as FmtWrite;
use std::fs::File;
use std::future::Future as Future03;
use std::io::{self, BufReader, Read, Write};
use std::io::{BufReader, Read, Write};
use std::iter::Peekable;
use std::mem::{forget, zeroed};
use std::num::{NonZeroUsize, TryFromIntError};
use std::ops::{Add, Deref, Div, RangeInclusive};
use std::os::raw::c_void;
use std::panic::{set_hook, PanicInfo};
use std::path::{Path, PathBuf};
use std::path::PathBuf;
use std::ptr::read_volatile;
use std::sync::atomic::Ordering;
use std::time::{Duration, SystemTime, SystemTimeError};
Expand Down Expand Up @@ -790,7 +790,7 @@ pub fn kdf_app_dir() -> Option<PathBuf> {
}

/// Returns path of the coins file.
pub fn kdf_coins_file() -> Result<PathBuf, io::Error> {
pub fn kdf_coins_file() -> PathBuf {
#[cfg(not(target_arch = "wasm32"))]
let value_from_env = env::var("MM_COINS_PATH").ok();

Expand All @@ -801,7 +801,7 @@ pub fn kdf_coins_file() -> Result<PathBuf, io::Error> {
}

/// Returns path of the config file.
pub fn kdf_config_file() -> Result<PathBuf, io::Error> {
pub fn kdf_config_file() -> PathBuf {
#[cfg(not(target_arch = "wasm32"))]
let value_from_env = env::var("MM_CONF_PATH").ok();

Expand All @@ -817,41 +817,16 @@ pub fn kdf_config_file() -> Result<PathBuf, io::Error> {
/// 1- From the environment variable.
/// 2- From the current directory where app is called.
/// 3- From the root application directory.
fn find_kdf_dependency_file(value_from_env: Option<String>, path_leaf: &str) -> Result<PathBuf, io::Error> {
pub fn find_kdf_dependency_file(value_from_env: Option<String>, path_leaf: &str) -> PathBuf {
if let Some(path) = value_from_env {
let path = PathBuf::from(path);
require_file(&path)?;
return Ok(path);
return PathBuf::from(path);
}

let from_current_dir = PathBuf::from(path_leaf);

let path = if from_current_dir.exists() {
if from_current_dir.exists() {
from_current_dir
} else {
kdf_app_dir().unwrap_or_default().join(path_leaf)
};

require_file(&path)?;
return Ok(path);

fn require_file(path: &Path) -> Result<(), io::Error> {
if path.is_dir() {
// TODO: use `IsADirectory` variant which is stabilized with 1.83
return Err(io::Error::new(
io::ErrorKind::InvalidInput,
format!("Expected file but '{}' is a directory.", path.display()),
));
}

if !path.exists() {
return Err(io::Error::new(
io::ErrorKind::NotFound,
format!("File '{}' is not present.", path.display()),
));
}

Ok(())
}
}

Expand Down
13 changes: 6 additions & 7 deletions mm2src/mm2_main/src/mm2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -304,23 +304,22 @@ pub fn mm2_main(version: String, datetime: String) {
/// Parses and returns the `first_arg` as JSON.
/// Attempts to load the config from `MM2.json` file if `first_arg` is None
pub fn get_mm2config(first_arg: Option<&str>) -> Result<Json, String> {
let conf_path = common::kdf_config_file();
let conf_from_file = slurp(&conf_path);
let conf = match first_arg {
Some(s) => s.to_owned(),
Some(s) => s,
None => {
let conf_path = common::kdf_config_file().map_err(|e| e.to_string())?;
let conf_from_file = slurp(&conf_path);

if conf_from_file.is_empty() {
return ERR!(
"Config is not set from command line arg and {} file doesn't exist.",
conf_path.display()
);
}
try_s!(String::from_utf8(conf_from_file))
try_s!(std::str::from_utf8(&conf_from_file))
},
};

let mut conf: Json = match json::from_str(&conf) {
let mut conf: Json = match json::from_str(conf) {
Ok(json) => json,
// Syntax or io errors may include the conf string in the error message so we don't want to take risks and show these errors internals in the log.
// If new variants are added to the Error enum, there can be a risk of exposing the conf string in the error message when updating serde_json so
Expand All @@ -329,7 +328,7 @@ pub fn get_mm2config(first_arg: Option<&str>) -> Result<Json, String> {
};

if conf["coins"].is_null() {
let coins_path = common::kdf_coins_file().map_err(|e| e.to_string())?;
let coins_path = common::kdf_coins_file();

let coins_from_file = slurp(&coins_path);
if coins_from_file.is_empty() {
Expand Down

0 comments on commit 908686c

Please sign in to comment.