@@ -373,18 +373,8 @@ bool AgencyReadTransaction::validate(AgencyCommResult const& result) const {
373
373
// --SECTION-- AgencyCommResult
374
374
// -----------------------------------------------------------------------------
375
375
376
- AgencyCommResult::AgencyCommResult ()
377
- : _location(), _message(), _body(), _values(), _statusCode(0 ), _connected(false ), _sent(false ) {}
378
-
379
- AgencyCommResult::AgencyCommResult (int code, std::string const & message,
380
- std::string const & clientId)
381
- : _location(),
382
- _message(message),
383
- _body(),
384
- _values(),
385
- _statusCode(code),
386
- _connected(false ),
387
- _sent(false ) {}
376
+ AgencyCommResult::AgencyCommResult (int code, std::string message)
377
+ : _message(std::move(message)), _statusCode(code) {}
388
378
389
379
AgencyCommResult::AgencyCommResult (AgencyCommResult&& other) noexcept
390
380
: _location(std::move(other._location)),
@@ -418,8 +408,8 @@ AgencyCommResult& AgencyCommResult::operator=(AgencyCommResult&& other) noexcept
418
408
return *this ;
419
409
}
420
410
421
- void AgencyCommResult::set (int code, std::string const & message) {
422
- _message = message;
411
+ void AgencyCommResult::set (int code, std::string message) {
412
+ _message = std::move ( message) ;
423
413
_statusCode = code;
424
414
_location.clear ();
425
415
_body.clear ();
@@ -434,45 +424,39 @@ int AgencyCommResult::httpCode() const { return _statusCode; }
434
424
bool AgencyCommResult::sent () const { return _sent; }
435
425
436
426
int AgencyCommResult::errorCode () const {
427
+ return asResult ().errorNumber ();
428
+ }
429
+
430
+ std::string AgencyCommResult::errorMessage () const {
431
+ return asResult ().errorMessage ();
432
+ }
433
+
434
+ std::optional<std::pair<int , std::string_view>> AgencyCommResult::parseBodyError () const {
435
+ auto result = std::optional<std::pair<int , std::string_view>>{};
437
436
try {
438
437
if (!_body.empty ()) {
439
438
std::shared_ptr<VPackBuilder> bodyBuilder = VPackParser::fromJson (_body);
440
439
VPackSlice body = bodyBuilder->slice ();
441
- if (!body.isObject ()) {
442
- return 0 ;
440
+ if (body.isObject ()) {
441
+ // get "errorCode" attribute
442
+ auto const errorCode = body.get (StaticStrings::ErrorCode).getNumber <int >();
443
+ // Save error code if possible, set default error message first
444
+ result = std::make_pair (errorCode, std::string_view (TRI_errno_string (errorCode)));
445
+ // Now try to extract the message, too; but it's fine if that fails, we
446
+ // already have the default one.
447
+ if (auto const errMsg = body.get (StaticStrings::ErrorMessage); errMsg.isString ()) {
448
+ auto const strRef = errMsg.stringRef ();
449
+ result->second = {strRef.begin (), strRef.size ()};
450
+ } else if (auto const errMsg = body.get (" message" ); errMsg.isString ()) {
451
+ auto const strRef = errMsg.stringRef ();
452
+ result->second = {strRef.begin (), strRef.size ()};
453
+ }
443
454
}
444
- // get "errorCode" attribute (0 if not exist)
445
- return basics::VelocyPackHelper::getNumericValue<int >(body, " errorCode" , 0 );
446
455
}
447
456
} catch (VPackException const &) {
448
457
}
449
- return 0 ;
450
- }
451
-
452
- std::string AgencyCommResult::errorMessage () const {
453
- if (!_message.empty ()) {
454
- // return stored message first if set
455
- return _message;
456
- }
457
-
458
- if (!_connected) {
459
- return std::string (" unable to connect to agency" );
460
- }
461
458
462
- try {
463
- std::shared_ptr<VPackBuilder> bodyBuilder = VPackParser::fromJson (_body);
464
-
465
- VPackSlice body = bodyBuilder->slice ();
466
- if (!body.isObject ()) {
467
- return " " ;
468
- }
469
- // get "message" attribute ("" if not exist)
470
- return arangodb::basics::VelocyPackHelper::getStringValue (body, " message" ,
471
- " " );
472
- } catch (VPackException const & e) {
473
- std::string message (" VPackException parsing body (" + _body + " ): " + e.what ());
474
- return std::string (message);
475
- }
459
+ return result;
476
460
}
477
461
478
462
std::string AgencyCommResult::errorDetails () const {
@@ -532,6 +516,24 @@ VPackBuilder AgencyCommResult::toVelocyPack() const {
532
516
return builder;
533
517
}
534
518
519
+ Result AgencyCommResult::asResult () const {
520
+ if (successful ()) {
521
+ return Result{};
522
+ } else if (auto const err = parseBodyError (); err.has_value ()) {
523
+ return Result{err->first , std::string{err->second }};
524
+ } else if (_statusCode > 0 ) {
525
+ if (!_message.empty ()) {
526
+ return Result{_statusCode, _message};
527
+ } else if (!_connected) {
528
+ return Result{_statusCode, " unable to connect to agency" };
529
+ } else {
530
+ return Result{_statusCode};
531
+ }
532
+ } else {
533
+ return Result{TRI_ERROR_INTERNAL};
534
+ }
535
+ }
536
+
535
537
namespace std {
536
538
ostream& operator << (ostream& out, AgencyCommResult const & a) {
537
539
out << a.toVelocyPack ().toJson ();
@@ -978,7 +980,7 @@ AgencyCommResult AgencyComm::getValues(std::string const& key) {
978
980
}
979
981
980
982
try {
981
- result.setVPack (VPackParser::fromJson (result.bodyRef ()));
983
+ result.setVPack (VPackParser::fromJson (result.body ()));
982
984
983
985
if (!result.slice ().isArray ()) {
984
986
result.set (500 , " got invalid result structure for getValues response" );
@@ -1020,8 +1022,7 @@ AgencyCommResult AgencyComm::dump() {
1020
1022
}
1021
1023
1022
1024
try {
1023
-
1024
- result.setVPack (VPackParser::fromJson (result.bodyRef ()));
1025
+ result.setVPack (VPackParser::fromJson (result.body ()));
1025
1026
result._body .clear ();
1026
1027
result._statusCode = 200 ;
1027
1028
@@ -1154,6 +1155,7 @@ uint64_t AgencyComm::uniqid(uint64_t count, double timeout) {
1154
1155
// The cas did not work, simply try again!
1155
1156
}
1156
1157
1158
+ TRI_ASSERT (oldValue != 0 );
1157
1159
return oldValue;
1158
1160
}
1159
1161
@@ -1252,14 +1254,14 @@ AgencyCommResult AgencyComm::sendTransactionWithFailover(AgencyTransaction const
1252
1254
}
1253
1255
1254
1256
try {
1255
- result.setVPack (VPackParser::fromJson (result.bodyRef ()));
1257
+ result.setVPack (VPackParser::fromJson (result.body ()));
1256
1258
1257
1259
if (!transaction.validate (result)) {
1258
1260
result.set (500 , std::string (" validation failed for response to URL " + url));
1259
1261
LOG_TOPIC (" f2083" , DEBUG, Logger::AGENCYCOMM)
1260
1262
<< " validation failed for url: " << url
1261
1263
<< " , type: " << transaction.typeName ()
1262
- << " , sent: " << builder.toJson () << " , received: " << result.bodyRef ();
1264
+ << " , sent: " << builder.toJson () << " , received: " << result.body ();
1263
1265
return result;
1264
1266
}
1265
1267
@@ -1269,7 +1271,7 @@ AgencyCommResult AgencyComm::sendTransactionWithFailover(AgencyTransaction const
1269
1271
LOG_TOPIC (" e13a5" , ERR, Logger::AGENCYCOMM)
1270
1272
<< " Error transforming result: " << e.what ()
1271
1273
<< " , status code: " << result._statusCode
1272
- << " , incriminating body: " << result.bodyRef () << " , url: " << url
1274
+ << " , incriminating body: " << result.body () << " , url: " << url
1273
1275
<< " , timeout: " << timeout << " , data sent: " << builder.toJson ();
1274
1276
result.clear ();
1275
1277
} catch (...) {
0 commit comments