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" name = "oxigraph_server"
version = "0.3.8" version = "0.3.8"
dependencies = [ dependencies = [
"anyhow",
"assert_cmd", "assert_cmd",
"assert_fs", "assert_fs",
"clap 4.0.18", "clap 4.0.18",

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

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

Loading…
Cancel
Save