error.dart 1.93 KB
Newer Older
Ian Hickson's avatar
Ian Hickson committed
1
// Copyright 2014 The Flutter Authors. All rights reserved.
2 3 4
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

5
import 'dart:io' show stderr, FileSystemException;
6 7 8

/// Standard error thrown by Flutter Driver API.
class DriverError extends Error {
9 10
  /// Create an error with a [message] and (optionally) the [originalError] and
  /// [originalStackTrace] that caused it.
11 12 13 14 15
  DriverError(this.message, [this.originalError, this.originalStackTrace]);

  /// Human-readable error message.
  final String message;

16
  /// The error object that was caught and wrapped by this error object, if any.
17
  final Object? originalError;
18

19
  /// The stack trace that was caught and wrapped by this error object, if any.
20
  final Object? originalStackTrace;
21

22
  @override
23
  String toString() {
24 25
    if (originalError == null)
      return 'DriverError: $message\n';
26 27
    return '''
DriverError: $message
28 29 30
Original error: $originalError
Original stack trace:
$originalStackTrace
31
''';
32
  }
33 34
}

35
/// Signature for [driverLog].
36
///
37 38
/// The first argument is a string representing the source of the message,
/// typically the class name or library name calling the method.
39
///
40 41
/// The second argument is the message being logged.
typedef DriverLogCallback = void Function(String source, String message);
42

43 44 45 46 47 48 49 50 51 52
/// Print the given message to the console.
///
/// The first argument is a string representing the source of the message.
///
/// The second argument is the message being logged.
///
/// This can be set to a different callback to override the handling of log
/// messages from the driver subsystem.
///
/// The default implementation prints `"$source: $message"` to stderr.
53 54 55
DriverLogCallback driverLog = _defaultDriverLogger;

void _defaultDriverLogger(String source, String message) {
56 57 58 59 60
  try {
    stderr.writeln('$source: $message');
  } on FileSystemException {
    // May encounter IO error: https://github.com/flutter/flutter/issues/69314
  }
61
}