resolveError method

  1. @override
Object resolveError(
  1. Object error,
  2. StackTrace stackTrace,
  3. Object? operation,
  4. Object? previousError,
)
override

Implementation

@override
Object resolveError(Object error, StackTrace stackTrace, Object? operation,
    Object? previousError) {
  if (error is DBMySQLAdapterException) {
    return error;
  } else if (error is MySqlException) {
    if (error.errorNumber == 1062) {
      var keyMatch = RegExp(r"for key '(?:(\w+)\.(.*?)|(.*?))'")
          .firstMatch(error.message);

      String? tableName;
      String? fieldName;

      if (keyMatch != null) {
        var mTable = keyMatch[1];
        var mField = keyMatch[2];
        var mKey = keyMatch[3];

        if (mTable != null) {
          tableName = mTable;
        }

        fieldName = mField ?? mKey;
      }

      return EntityFieldInvalid("unique", error.message,
          tableName: tableName,
          fieldName: fieldName,
          parentError: error,
          parentStackTrace: stackTrace,
          previousError: previousError);
    }
  }

  return DBMySQLAdapterException('error', '$error',
      parentError: error,
      parentStackTrace: stackTrace,
      previousError: previousError,
      operation: operation);
}