error.dart 1.94 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 FileSystemException, stderr;
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
    if (originalError == null) {
25
      return 'DriverError: $message\n';
26
    }
27 28
    return '''
DriverError: $message
29 30 31
Original error: $originalError
Original stack trace:
$originalStackTrace
32
''';
33
  }
34 35
}

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

44 45 46 47 48 49 50 51 52 53
/// 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.
54 55 56
DriverLogCallback driverLog = _defaultDriverLogger;

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