Skip to content

Commit

Permalink
fix(error): add proper context to error messages
Browse files Browse the repository at this point in the history
NLVAsyncWorkerBase was previously modified to include context for
error location when libvirt operations failed. This didn't
translate over to the most recent async worker work, and therefore
error context was consistently showing as being called from
`nlv_async_worker.cc:58`, or the callsite of the error callback of
the async worker. This patch corrects this behavior
  • Loading branch information
mbroadst committed Nov 9, 2016
1 parent 05040cd commit c9f159e
Show file tree
Hide file tree
Showing 17 changed files with 168 additions and 164 deletions.
94 changes: 47 additions & 47 deletions src/domain.cc

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion src/domain.h
Original file line number Diff line number Diff line change
Expand Up @@ -473,7 +473,7 @@ class Domain : public NLVObject<Domain, virDomainPtr, DomainCleanupHandler>
NLV_WORKER_ASSERT_DOMAIN();
virErrorPtr error = func_(Handle());
if(error) {
SetVirError(error);
SET_ERROR_WITH_CONTEXT(error);
}
}
private:
Expand Down
15 changes: 6 additions & 9 deletions src/error.cc
Original file line number Diff line number Diff line change
Expand Up @@ -148,24 +148,22 @@ void Error::Initialize(Handle<Object> exports)
NODE_DEFINE_CONSTANT(exports, VIR_ERR_ERROR);
}

Error::Error(virErrorPtr error, const char* context)
: Nan::ObjectWrap()
Error::Error(virErrorPtr error, std::string context)
: Nan::ObjectWrap(),
error_(error),
context_(context)
{
error_ = error;
context_ = context;
}

Error::~Error()
{
virFreeError(error_);
}

Local<Value> Error::New(virErrorPtr error, const char* context)
Local<Value> Error::New(virErrorPtr error, std::string context)
{
Nan::EscapableHandleScope scope;

Local<Function> ctor = Nan::New<Function>(constructor);

Local<Object> instance = Nan::NewInstance(ctor).ToLocalChecked();
Error *err = new Error(error, context);
err->Wrap(instance);
Expand All @@ -175,7 +173,6 @@ Local<Value> Error::New(virErrorPtr error, const char* context)
NAN_GETTER(Error::Getter)
{
Nan::HandleScope scope;

Error *error = Nan::ObjectWrap::Unwrap<Error>(info.This());
virErrorPtr error_ = error->error_;

Expand All @@ -198,7 +195,7 @@ NAN_GETTER(Error::Getter)
} else if (property->Equals(Nan::New("int2").ToLocalChecked())) {
return info.GetReturnValue().Set(Nan::New(error_->int2));
} else if (property->Equals(Nan::New("context").ToLocalChecked())) {
return info.GetReturnValue().Set(Nan::New(error->context_).ToLocalChecked());
return info.GetReturnValue().Set(Nan::New(error->context_.c_str()).ToLocalChecked());
}

return;
Expand Down
13 changes: 7 additions & 6 deletions src/error.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,25 +10,26 @@ class Error : public Nan::ObjectWrap
{
public:
static void Initialize(Handle<Object> exports);
static Local<Value> New(virErrorPtr error, const char* context);
static Local<Value> New(virErrorPtr error, std::string context);

private:
explicit Error(virErrorPtr error, const char* context);
explicit Error(virErrorPtr error, std::string context);
~Error();
static Nan::Persistent<Function> constructor;

static NAN_GETTER(Getter);

virErrorPtr error_;
const char* context_;
std::string context_;
};

} //namespace NLV

#define _LIBVIRT_STRINGIZE_DETAIL(x) #x
#define _LIBVIRT_STRINGIZE(x) _LIBVIRT_STRINGIZE_DETAIL(x)

#define ThrowLastVirError() Nan::ThrowError(Error::New(virSaveLastError(), __FILE__ ":" _LIBVIRT_STRINGIZE(__LINE__)))
#define MakeVirError() Error::New(VirError(), __FILE__ ":" _LIBVIRT_STRINGIZE(__LINE__))
#define ThrowLastVirError() \
Nan::ThrowError(Error::New(virSaveLastError(), __FILE__ ":" _LIBVIRT_STRINGIZE(__LINE__)))
#define SET_ERROR_WITH_CONTEXT(error) \
SetVirError(error, __FILE__ ":" _LIBVIRT_STRINGIZE(__LINE__))

#endif // ERROR_H
44 changes: 22 additions & 22 deletions src/hypervisor.cc
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,7 @@ NLV_WORKER_EXECUTE(Hypervisor, Disconnect)
hypervisor_->ClearHandle();
// int result = virConnectClose(Handle());
// if (result == -1) {
// SetVirError(virSaveLastError());
// SET_ERROR_WITH_CONTEXT(virSaveLastError());
// return;
// }

Expand All @@ -309,7 +309,7 @@ NLV_WORKER_EXECUTE(Hypervisor, Disconnect)
NLV_WORKER_ASSERT_CONNECTION(); \
char *result = Accessor(Handle()); \
if (result == NULL) { \
SetVirError(virSaveLastError()); \
SET_ERROR_WITH_CONTEXT(virSaveLastError()); \
return; \
} \
data_ = result; \
Expand All @@ -321,7 +321,7 @@ NLV_WORKER_EXECUTE(Hypervisor, Disconnect)
NLV_WORKER_ASSERT_CONNECTION(); \
const char *result = Accessor(Handle()); \
if (result == NULL) { \
SetVirError(virSaveLastError()); \
SET_ERROR_WITH_CONTEXT(virSaveLastError()); \
return; \
} \
data_ = result; \
Expand All @@ -340,7 +340,7 @@ NLV_WORKER_EXECUTE(Hypervisor, GetSysInfo)
NLV_WORKER_ASSERT_CONNECTION();
char *result = virConnectGetSysinfo(Handle(), 0);
if (result == NULL) {
SetVirError(virSaveLastError());
SET_ERROR_WITH_CONTEXT(virSaveLastError());
return;
}

Expand All @@ -362,7 +362,7 @@ NLV_WORKER_EXECUTE(Hypervisor, GetVersion)
unsigned long version;
int result = virConnectGetVersion(Handle(), &version);
if (result == -1) {
SetVirError(virSaveLastError());
SET_ERROR_WITH_CONTEXT(virSaveLastError());
return;
} else if (result == 0 && version == 0) {
return;
Expand All @@ -386,7 +386,7 @@ NLV_WORKER_EXECUTE(Hypervisor, GetLibVirtVersion)
unsigned long version;
int result = virConnectGetLibVersion(Handle(), &version);
if (result == -1) {
SetVirError(virSaveLastError());
SET_ERROR_WITH_CONTEXT(virSaveLastError());
return;
}

Expand All @@ -404,7 +404,7 @@ NLV_WORKER_EXECUTE(Hypervisor, GetLibVirtVersion)
NLV_WORKER_ASSERT_CONNECTION(); \
int result = Accessor(Handle()); \
if (result == -1) { \
SetVirError(virSaveLastError()); \
SET_ERROR_WITH_CONTEXT(virSaveLastError()); \
return; \
} \
data_ = result; \
Expand Down Expand Up @@ -450,7 +450,7 @@ NLV_WORKER_EXECUTE(Hypervisor, GetMaxVcpus)
NLV_WORKER_ASSERT_CONNECTION();
int result = virConnectGetMaxVcpus(Handle(), type_.c_str());
if (result == -1) {
SetVirError(virSaveLastError());
SET_ERROR_WITH_CONTEXT(virSaveLastError());
return;
}

Expand Down Expand Up @@ -496,7 +496,7 @@ NLV_WORKER_EXECUTE(Hypervisor, SetKeepAlive)

int result = virConnectSetKeepAlive(Handle(), interval_, count_);
if (result == -1) {
SetVirError(virSaveLastError());
SET_ERROR_WITH_CONTEXT(virSaveLastError());
return;
}

Expand Down Expand Up @@ -540,7 +540,7 @@ NLV_WORKER_EXECUTE(Hypervisor, GetBaselineCPU)
delete [] cpus_;

if (result == NULL) {
SetVirError(virSaveLastError());
SET_ERROR_WITH_CONTEXT(virSaveLastError());
return;
}

Expand Down Expand Up @@ -570,7 +570,7 @@ NLV_WORKER_EXECUTE(Hypervisor, CompareCPU)
NLV_WORKER_ASSERT_CONNECTION();
int result = virConnectCompareCPU(Handle(), (const char *)cpu_.c_str(), flags_);
if (result == VIR_CPU_COMPARE_ERROR) {
SetVirError(virSaveLastError());
SET_ERROR_WITH_CONTEXT(virSaveLastError());
return;
}

Expand All @@ -582,7 +582,7 @@ NLV_WORKER_EXECUTE(Hypervisor, CompareCPU)
NLV_WORKER_ASSERT_CONNECTION() \
int count = CountMethod(Handle()); \
if (count == -1) { \
SetVirError(virSaveLastError()); \
SET_ERROR_WITH_CONTEXT(virSaveLastError()); \
return; \
} \
char **names = new char*[count]; \
Expand All @@ -592,7 +592,7 @@ NLV_WORKER_EXECUTE(Hypervisor, CompareCPU)
} \
int nameCount = ListMethod(Handle(), names, count); \
if (nameCount == -1) { \
SetVirError(virSaveLastError()); \
SET_ERROR_WITH_CONTEXT(virSaveLastError()); \
delete [] names; \
return; \
} \
Expand All @@ -608,7 +608,7 @@ NLV_WORKER_EXECUTE(Hypervisor, CompareCPU)
NLV_WORKER_ASSERT_CONNECTION() \
int count = CountMethod(Handle()); \
if (count == -1) { \
SetVirError(virSaveLastError()); \
SET_ERROR_WITH_CONTEXT(virSaveLastError()); \
return; \
} \
int *elements = new int[count]; \
Expand All @@ -619,7 +619,7 @@ NLV_WORKER_EXECUTE(Hypervisor, CompareCPU)
} \
int elementCount = ListMethod(Handle(), elements, count); \
if (elementCount == -1) { \
SetVirError(virSaveLastError()); \
SET_ERROR_WITH_CONTEXT(virSaveLastError()); \
return; \
} \
for (int i = 0; i < elementCount; ++i) { \
Expand Down Expand Up @@ -730,7 +730,7 @@ NLV_WORKER_EXECUTE(Hypervisor, ListNodeDevices)
virNodeNumOfDevices(Handle(), (const char *) capability_.c_str(), flags);

if (num_devices == -1) {
SetVirError(virSaveLastError());
SET_ERROR_WITH_CONTEXT(virSaveLastError());
return;
}

Expand All @@ -744,7 +744,7 @@ NLV_WORKER_EXECUTE(Hypervisor, ListNodeDevices)
virNodeListDevices(Handle(), (const char *)capability_.c_str(), names, num_devices, flags);
if (num_devices == -1) {
free(names);
SetVirError(virSaveLastError());
SET_ERROR_WITH_CONTEXT(virSaveLastError());
return;
}

Expand All @@ -762,7 +762,7 @@ NLV_WORKER_EXECUTE(Hypervisor, GetNodeSecurityModel)
NLV_WORKER_ASSERT_CONNECTION();
int result = virNodeGetSecurityModel(Handle(), &securityModel_);
if (result == -1) {
SetVirError(virSaveLastError());
SET_ERROR_WITH_CONTEXT(virSaveLastError());
return;
}
}
Expand All @@ -789,7 +789,7 @@ NLV_WORKER_EXECUTE(Hypervisor, GetNodeInfo)
NLV_WORKER_ASSERT_CONNECTION();
int result = virNodeGetInfo(Handle(), &info_);
if (result == -1) {
SetVirError(virSaveLastError());
SET_ERROR_WITH_CONTEXT(virSaveLastError());
return;
}
}
Expand Down Expand Up @@ -818,7 +818,7 @@ NLV_WORKER_EXECUTE(Hypervisor, GetNodeFreeMemory)
NLV_WORKER_ASSERT_CONNECTION();
unsigned long long result = virNodeGetFreeMemory(Handle());
if (result == 0) {
SetVirError(virSaveLastError());
SET_ERROR_WITH_CONTEXT(virSaveLastError());
return;
}

Expand Down Expand Up @@ -851,7 +851,7 @@ NLV_WORKER_EXECUTE(Hypervisor, GetNodeMemoryStats)
result = virNodeGetMemoryStats(Handle(), cellNum_, &info_[0], &nparams, flags_);
}
if (result == -1) {
SetVirError(virSaveLastError());
SET_ERROR_WITH_CONTEXT(virSaveLastError());
return;
}
}
Expand Down Expand Up @@ -904,7 +904,7 @@ NLV_WORKER_EXECUTE(Hypervisor, GetNodeCellsFreeMemory)
virNodeGetCellsFreeMemory(Handle(), results, startCell_, maxCells_);
if (cells == -1) {
free(results);
SetVirError(virSaveLastError());
SET_ERROR_WITH_CONTEXT(virSaveLastError());
return;
}

Expand Down
16 changes: 8 additions & 8 deletions src/interface.cc
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ NLV_WORKER_EXECUTE(Interface, Start)
unsigned int flags = 0;
int result = virInterfaceCreate(Handle(), flags);
if (result == -1) {
SetVirError(virSaveLastError());
SET_ERROR_WITH_CONTEXT(virSaveLastError());
return;
}

Expand All @@ -55,7 +55,7 @@ NLV_WORKER_EXECUTE(Interface, Stop)
unsigned int flags = 0;
int result = virInterfaceDestroy(Handle(), flags);
if (result == -1) {
SetVirError(virSaveLastError());
SET_ERROR_WITH_CONTEXT(virSaveLastError());
return;
}

Expand All @@ -68,7 +68,7 @@ NLV_WORKER_EXECUTE(Interface, Define)
unsigned int flags = 0;
lookupHandle_ = virInterfaceDefineXML(parent_->virHandle(), value_.c_str(), flags);
if (lookupHandle_ == NULL) {
SetVirError(virSaveLastError());
SET_ERROR_WITH_CONTEXT(virSaveLastError());
return;
}
}
Expand All @@ -79,7 +79,7 @@ NLV_WORKER_EXECUTE(Interface, Undefine)
NLV_WORKER_ASSERT_INTERFACE();
int result = virInterfaceUndefine(Handle());
if (result == -1) {
SetVirError(virSaveLastError());
SET_ERROR_WITH_CONTEXT(virSaveLastError());
return;
}

Expand Down Expand Up @@ -138,7 +138,7 @@ NLV_WORKER_EXECUTE(Interface, GetName)
NLV_WORKER_ASSERT_INTERFACE();
const char *result = virInterfaceGetName(Handle());
if (result == NULL) {
SetVirError(virSaveLastError());
SET_ERROR_WITH_CONTEXT(virSaveLastError());
return;
}

Expand All @@ -151,7 +151,7 @@ NLV_WORKER_EXECUTE(Interface, GetMacAddress)
NLV_WORKER_ASSERT_INTERFACE();
const char *result = virInterfaceGetMACString(Handle());
if (result == NULL) {
SetVirError(virSaveLastError());
SET_ERROR_WITH_CONTEXT(virSaveLastError());
return;
}

Expand All @@ -164,7 +164,7 @@ NLV_WORKER_EXECUTE(Interface, IsActive)
NLV_WORKER_ASSERT_INTERFACE();
int result = virInterfaceIsActive(Handle());
if (result == -1) {
SetVirError(virSaveLastError());
SET_ERROR_WITH_CONTEXT(virSaveLastError());
return;
}

Expand All @@ -178,7 +178,7 @@ NLV_WORKER_EXECUTE(Interface, ToXml)
unsigned int flags = 0;
char *result = virInterfaceGetXMLDesc(Handle(), flags);
if (result == NULL) {
SetVirError(virSaveLastError());
SET_ERROR_WITH_CONTEXT(virSaveLastError());
return;
}

Expand Down
Loading

0 comments on commit c9f159e

Please sign in to comment.