BDE 4.39.x Production Release
Loading...
Searching...
No Matches
bdljsn_error

Detailed Description

Provide a description of an error processing a document.

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:

/// 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.
int extractIntegerToken(int *value,
bdljsn::Error *error,
bsl::string_view inputText)
{
BSLS_ASSERT(value);
BSLS_ASSERT(error);
enum { e_SUCCESS, e_FAILURE };
// ...
Definition bdljsn_error.h:155
Definition bslstl_stringview.h:471
#define BSLS_ASSERT(X)
Definition bsls_assert.h:1976

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

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

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:412
Error & setLocation(const Location &value)
Set the location attribute of this object to the specified value.
Definition bdljsn_error.h:405
Definition bdljsn_location.h:168
TYPE first
Definition bslstl_pair.h:587
TYPE second
Definition bslstl_pair.h:933

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

*value = result;
return e_SUCCESS;
}