Uses anyhow for errors

pull/299/head
Tpt 2 years ago committed by Thomas Tanon
parent ea4ae6bc48
commit c9762fc280
  1. 1
      Cargo.lock
  2. 1
      server/Cargo.toml
  3. 31
      server/src/main.rs

1
Cargo.lock generated

@ -932,6 +932,7 @@ dependencies = [
name = "oxigraph_server"
version = "0.3.8"
dependencies = [
"anyhow",
"assert_cmd",
"assert_fs",
"clap 4.0.18",

@ -12,6 +12,7 @@ Oxigraph SPARQL HTTP server
edition = "2021"
[dependencies]
anyhow = "1"
oxhttp = { version = "0.1", features = ["rayon"] }
clap = { version = "4", features = ["derive"] }
oxigraph = { version = "0.3.8", path = "../lib", features = ["http_client"] }

@ -1,3 +1,4 @@
use anyhow::anyhow;
use clap::{Parser, Subcommand};
use flate2::read::MultiGzDecoder;
use oxhttp::model::{Body, HeaderName, HeaderValue, Request, Response, Status};
@ -15,7 +16,7 @@ use std::cmp::{max, min};
use std::ffi::OsStr;
use std::fmt;
use std::fs::File;
use std::io::{self, BufReader, Error, ErrorKind, Read, Write};
use std::io::{self, BufReader, Read, Write};
use std::path::{Path, PathBuf};
use std::rc::Rc;
use std::str::FromStr;
@ -62,7 +63,7 @@ enum Command {
},
}
pub fn main() -> io::Result<()> {
pub fn main() -> anyhow::Result<()> {
let matches = Args::parse();
let store = if let Some(path) = &matches.location {
Store::open(path)
@ -75,8 +76,7 @@ pub fn main() -> io::Result<()> {
ThreadPoolBuilder::new()
.num_threads(max(1, available_parallelism()?.get() / 2))
.thread_name(|i| format!("Oxigraph bulk loader thread {}", i))
.build()
.map_err(|e| Error::new(ErrorKind::Other, e))?
.build()?
.scope(|s| {
for file in file {
let store = store.clone();
@ -142,10 +142,16 @@ pub fn main() -> io::Result<()> {
}
}
fn bulk_load(loader: BulkLoader, reader: impl Read, file: &Path) -> io::Result<()> {
let extension = file.extension().and_then(|extension| extension.to_str()).ok_or_else(|| Error::new(
ErrorKind::InvalidInput,
format!("The server is not able to guess the file format of {} because the file name as no extension", file.display())))?;
fn bulk_load(loader: BulkLoader, reader: impl Read, file: &Path) -> anyhow::Result<()> {
let extension = file
.extension()
.and_then(|extension| extension.to_str())
.ok_or_else(|| {
anyhow!(
"Not able to guess the file format of {} because the file name as no extension",
file.display()
)
})?;
let reader = BufReader::new(reader);
if let Some(format) = DatasetFormat::from_extension(extension) {
loader.load_dataset(reader, format, None)?;
@ -154,12 +160,9 @@ fn bulk_load(loader: BulkLoader, reader: impl Read, file: &Path) -> io::Result<(
loader.load_graph(reader, format, GraphNameRef::DefaultGraph, None)?;
Ok(())
} else {
Err(Error::new(
ErrorKind::InvalidInput,
format!(
"The server is not able to guess the file format from the extension {}",
extension
),
Err(anyhow!(
"Not able to guess the file format from the extension {}",
extension
))
}
}

Loading…
Cancel
Save