BDE 4.14.0 Production release
Loading...
Searching...
No Matches
bdljsn_error

Detailed Description

Outline

Purpose

Provide a description of an error processing a document.

Classes

See also
bdljsn_jsonutil, bdljsn_json

Description

This component provides a single, un-constrained (value-semantic) attribute class, bdljsn::Error, that is used to describe an error in the occured processing a (JSON) document.

Attributes

Name Type Default
------------------ ----------- -------
location Location Location(0)
message string ""

Usage

This section illustrates intended use of this component.

Example 1: Populating an bdljsn::Error Object

This component is designed to describe an error that occured when processing a (JSON) document. Suppose we are implementing a function, extractIntegerToken, that parses a numeric token and obtains an int value:

First, we define the function signature:

int extractIntegerToken(int *value,
bdljsn::Error *error,
bsl::string_view inputText)
// Load to the specified 'value' the 'int' value represented by the
// specified 'inputText'. Return 0 on success, and a non-zero value
// otherwise with no effect on '*value' and the specified 'error' is
// set.
{
BSLS_ASSERT(value);
BSLS_ASSERT(error);
enum { e_SUCCESS, e_FAILURE };
// ...
Definition bdljsn_error.h:157
Definition bslstl_stringview.h:441
#define BSLS_ASSERT(X)
Definition bsls_assert.h:1804

Then, we attempt to exact a int value from the inputText:

int result;
MyNumericUtil::parseInt(&result, inputText);
Definition bslstl_pair.h:1210

Now, we check the parse status and if unsuccessful, we use the status information to set the bsljsn::Error object expected by our caller:

if (MyParseStatus::e_OK != status.first) {
unsigned position = status.second;
error->setLocation(bdljsn::Location(static_cast<bsl::uint64_t>(
position)));
error->setMessage(MyParseStatus::toAscii(status.first));
return e_FAILURE; // RETURN
}
Error & setMessage(const bsl::string_view &value)
Set the message attribute of this object to the specified value.
Definition bdljsn_error.h:413
Error & setLocation(const Location &value)
Definition bdljsn_error.h:406
Definition bdljsn_location.h:168
TYPE first
Definition bslstl_pair.h:605
TYPE second
Definition bslstl_pair.h:908

Finally, if the parse was successful, set the output parameter and return with status value that indicates success.

*value = result;
return e_SUCCESS;
}