Skip to content

Commit a696c1c

Browse files
committed
[lldb-dap] Adding a DAPError for showing users error messages.
The `DAPError` can be used to craft an error message that is displayed to a user (with showUser=true). Any request handler implementation using subclassing `RequestHandler<>` should be able to use this. I updated SourceRequestHandler to report DAPError's specifically.
1 parent bc9cee1 commit a696c1c

File tree

5 files changed

+83
-9
lines changed

5 files changed

+83
-9
lines changed

lldb/tools/lldb-dap/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ add_lldb_tool(lldb-dap
1818
Breakpoint.cpp
1919
BreakpointBase.cpp
2020
DAP.cpp
21+
DAPError.cpp
2122
DAPLog.cpp
2223
EventHelper.cpp
2324
ExceptionBreakpoint.cpp

lldb/tools/lldb-dap/DAPError.cpp

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
//===-- DAPError.cpp ------------------------------------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#include "DAPError.h"
10+
#include "llvm/Support/Error.h"
11+
#include <system_error>
12+
13+
namespace lldb_dap {
14+
15+
char DAPError::ID;
16+
17+
DAPError::DAPError(std::string message, bool show_user)
18+
: m_message(message), m_show_user(show_user) {}
19+
20+
void DAPError::log(llvm::raw_ostream &OS) const { OS << m_message; }
21+
22+
std::error_code DAPError::convertToErrorCode() const {
23+
return llvm::inconvertibleErrorCode();
24+
}
25+
26+
} // namespace lldb_dap

lldb/tools/lldb-dap/DAPError.h

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
//===-- DAPError.h --------------------------------------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#include "llvm/Support/Error.h"
10+
#include <string>
11+
12+
namespace lldb_dap {
13+
14+
/// An Error that is reported as a DAP Error Message, which may be presented to
15+
/// the user.
16+
class DAPError : public llvm::ErrorInfo<DAPError> {
17+
public:
18+
static char ID;
19+
20+
DAPError(std::string message, bool show_user = false);
21+
22+
void log(llvm::raw_ostream &OS) const override;
23+
std::error_code convertToErrorCode() const override;
24+
25+
const std::string &getMessage() const { return m_message; }
26+
bool getShowUser() const { return m_show_user; }
27+
28+
private:
29+
std::string m_message;
30+
bool m_show_user;
31+
};
32+
33+
} // namespace lldb_dap

lldb/tools/lldb-dap/Handler/RequestHandler.h

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,13 @@
1010
#define LLDB_TOOLS_LLDB_DAP_HANDLER_HANDLER_H
1111

1212
#include "DAP.h"
13+
#include "DAPError.h"
1314
#include "DAPLog.h"
1415
#include "Protocol/ProtocolBase.h"
1516
#include "Protocol/ProtocolRequests.h"
1617
#include "lldb/API/SBError.h"
1718
#include "llvm/ADT/StringRef.h"
19+
#include "llvm/Support/Error.h"
1820
#include "llvm/Support/JSON.h"
1921
#include <optional>
2022
#include <type_traits>
@@ -118,20 +120,32 @@ class RequestHandler : public BaseRequestHandler {
118120
std::string parse_failure;
119121
llvm::raw_string_ostream OS(parse_failure);
120122
root.printErrorContext(request.arguments, OS);
123+
124+
protocol::ErrorMessage error;
125+
error.format = parse_failure;
126+
121127
response.success = false;
122-
response.message = parse_failure;
128+
response.body = std::move(error);
129+
123130
dap.Send(response);
124131
return;
125132
}
126133

127-
auto body = Run(arguments);
128-
// FIXME: Add a dedicated DAPError for enhanced errors that are
129-
// user-visibile.
134+
llvm::Expected<Body> body = Run(arguments);
130135
if (auto Err = body.takeError()) {
136+
protocol::ErrorMessage error;
137+
if (llvm::Error unhandled = llvm::handleErrors(
138+
std::move(Err), [&](const DAPError &E) -> llvm::Error {
139+
error.format = E.getMessage();
140+
error.showUser = E.getShowUser();
141+
error.id = E.convertToErrorCode().value();
142+
// TODO: We could add url/urlLabel to the error message for more
143+
// information for users.
144+
return llvm::Error::success();
145+
}))
146+
error.format = llvm::toString(std::move(unhandled));
131147
response.success = false;
132-
// FIXME: Build ErrorMessage based on error details instead of using the
133-
// 'message' field.
134-
response.message = llvm::toString(std::move(Err));
148+
response.body = std::move(error);
135149
} else {
136150
response.success = true;
137151
if constexpr (!std::is_same_v<Body, std::monostate>)

lldb/tools/lldb-dap/Handler/SourceRequestHandler.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ SourceRequestHandler::Run(const protocol::SourceArguments &args) const {
2929
args.source->sourceReference.value_or(args.sourceReference);
3030

3131
if (!source)
32-
return llvm::createStringError(
32+
return llvm::make_error<DAPError>(
3333
"invalid arguments, expected source.sourceReference to be set");
3434

3535
lldb::SBProcess process = dap.target.GetProcess();
@@ -39,7 +39,7 @@ SourceRequestHandler::Run(const protocol::SourceArguments &args) const {
3939
// Lower 32 bits is the frame index
4040
lldb::SBFrame frame = thread.GetFrameAtIndex(GetLLDBFrameID(source));
4141
if (!frame.IsValid())
42-
return llvm::createStringError("source not found");
42+
return llvm::make_error<DAPError>("source not found");
4343

4444
lldb::SBInstructionList insts = frame.GetSymbol().GetInstructions(dap.target);
4545
lldb::SBStream stream;

0 commit comments

Comments
 (0)