normalizeOdbcError function

OdbcError normalizeOdbcError(
  1. Object error, {
  2. required String operation,
  3. StackTrace? stackTrace,
})

Infrastructure exceptions are converted to OdbcError before this boundary.

Implementation

OdbcError normalizeOdbcError(
  Object error, {
  required String operation,
  StackTrace? stackTrace,
}) {
  if (error is OdbcErrorConvertible) {
    return normalizeOdbcError(
      error.toOdbcError(),
      operation: operation,
      stackTrace: stackTrace,
    );
  }
  if (error is OdbcError) {
    return error.withDetails(
      error.details.copyWith(
        operation: error.details.operation ?? operation,
        stackTrace: error.details.stackTrace ?? stackTrace,
      ),
      message:
          error.message.trim().isEmpty || error.message.trim() == 'No error'
              ? 'Failed to complete $operation'
              : null,
    );
  }
  final details = OdbcErrorDetails(
    operation: operation,
    cause: error,
    stackTrace: stackTrace,
  );
  if (error is TimeoutException) {
    return QueryError(
      message: 'The operation timed out',
      details: details.copyWith(
        code: OdbcErrorCode.timeout,
        outcomeUnknown: true,
      ),
    );
  }
  if (error is FormatException) {
    return MalformedPayloadError(
      message: error.message.trim().isEmpty
          ? 'Invalid response during $operation'
          : error.message,
      details: details.copyWith(code: OdbcErrorCode.protocol),
    );
  }
  if (error is UnsupportedError) {
    return UnsupportedFeatureError(
      message: 'The operation is not supported',
      details: details.copyWith(code: OdbcErrorCode.unsupported),
    );
  }
  return QueryError(
    message: 'Unexpected failure during $operation',
    details: details.copyWith(code: OdbcErrorCode.internal),
  );
}