Skip to content

Commit

Permalink
use exceptions
Browse files Browse the repository at this point in the history
  • Loading branch information
inclyc committed Aug 10, 2024
1 parent 80564c1 commit 90fca74
Show file tree
Hide file tree
Showing 7 changed files with 78 additions and 20 deletions.
2 changes: 2 additions & 0 deletions nixd/include/nixd/CommandLine/Configuration.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

#include "nixd/Controller/Configuration.h"

#include <exception>

namespace nixd {

/// \brief Parse the CLI flag and initialize the config nixd::DefaultConfig
Expand Down
18 changes: 18 additions & 0 deletions nixd/include/nixd/Support/Exception.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#pragma once

#include <llvm/Support/Error.h>

#include <exception>

namespace nixd {

class LLVMErrorException : public std::exception {
llvm::Error E;

public:
LLVMErrorException(llvm::Error E) : E(std::move(E)) {}

llvm::Error takeError() { return std::move(E); }
};

} // namespace nixd
38 changes: 38 additions & 0 deletions nixd/include/nixd/Support/JSON.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#include "Exception.h"

#include <llvm/ADT/StringRef.h>
#include <llvm/Support/JSON.h>

#include <exception>

namespace nixd {

class JSONParseException : public LLVMErrorException {
public:
JSONParseException(llvm::Error E) : LLVMErrorException(std::move(E)) {}

[[nodiscard]] const char *what() const noexcept override {
return "JSON result cannot be parsed";
}
};

class JSONSchemaException : public LLVMErrorException {
public:
JSONSchemaException(llvm::Error E) : LLVMErrorException(std::move(E)) {}

[[nodiscard]] const char *what() const noexcept override {
return "JSON schema mismatch";
}
};

llvm::json::Value parse(llvm::StringRef JSON);

template <class T> T fromJSON(const llvm::json::Value &V) {
llvm::json::Path::Root R;
T Result;
if (!fromJSON(V, Result, R))
throw JSONSchemaException(R.getError());
return Result;
}

} // namespace nixd
21 changes: 2 additions & 19 deletions nixd/lib/CommandLine/Configuration.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include "nixd/CommandLine/Configuration.h"
#include "nixd/CommandLine/Options.h"
#include "nixd/Controller/Configuration.h"
#include "nixd/Support/JSON.h"

#include "lspserver/Logger.h"

Expand All @@ -28,23 +29,5 @@ Configuration nixd::parseCLIConfig() {
if (DefaultConfigJSON.empty())
return {};

llvm::Expected<llvm::json::Value> V = llvm::json::parse(DefaultConfigJSON);

if (!V) {
llvm::errs()
<< "The JSON string of default configuration cannot be parsed, reason: "
<< V.takeError() << "\n";
std::exit(-1);
}

llvm::json::Path::Root R;
Configuration C;
if (!fromJSON(*V, C, R)) {
// JSON is valid, but it has incorrect schema
llvm::errs()
<< "The JSON string of default configuration has invalid schema: "
<< R.getError() << "\n";
std::exit(-1);
}
return C;
return nixd::fromJSON<Configuration>(nixd::parse(DefaultConfigJSON));
}
8 changes: 7 additions & 1 deletion nixd/lib/Controller/LifeTime.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include "nixd/Eval/Launch.h"

#include "lspserver/Protocol.h"
#include "nixd/Support/Exception.h"

#include <llvm/Support/CommandLine.h>

Expand Down Expand Up @@ -184,7 +185,12 @@ void Controller::
evalExprWithProgress(*Client, getDefaultNixOSOptionsExpr(),
"nixos options");
}
Config = parseCLIConfig();
try {
Config = parseCLIConfig();
} catch (LLVMErrorException &Err) {
lspserver::elog("{0}, reason: {1}", Err.what(), Err.takeError());
std::exit(-1);
}
fetchConfig();
}

Expand Down
10 changes: 10 additions & 0 deletions nixd/lib/Support/JSON.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#include "nixd/Support/JSON.h"

#include <llvm/ADT/StringRef.h>

llvm::json::Value nixd::parse(llvm::StringRef JSON) {
llvm::Expected<llvm::json::Value> E = llvm::json::parse(JSON);
if (!E)
throw JSONParseException(E.takeError());
return *E;
}
1 change: 1 addition & 0 deletions nixd/lib/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ libnixd_lib = library(
'Support/AutoCloseFD.cpp',
'Support/AutoRemoveShm.cpp',
'Support/ForkPiped.cpp',
'Support/JSON.cpp',
'Support/StreamProc.cpp',
dependencies: libnixd_deps,
include_directories: libnixd_include,
Expand Down

0 comments on commit 90fca74

Please sign in to comment.