withMessage method

SmartError withMessage(
  1. String message
)

返回一个仅替换了 message 的同类型错误实例。

其余字段会被完整保留(如 requestOptions、stackTrace、timestamp 等)。

Implementation

SmartError withMessage(String message) => switch (this) {
  NetworkError(:final host, :final errorCode) => NetworkError(
    message: message,
    host: host,
    errorCode: errorCode,
    requestOptions: requestOptions,
    stackTrace: stackTrace,
    originalError: originalError,
    timestamp: timestamp,
  ),
  TimeoutError(:final phase, :final timeout) => TimeoutError(
    message: message,
    phase: phase,
    timeout: timeout,
    requestOptions: requestOptions,
    stackTrace: stackTrace,
    originalError: originalError,
    timestamp: timestamp,
  ),
  ResponseError(
    :final statusCode,
    :final statusMessage,
    :final responseHeaders,
    :final responseData,
  ) =>
    ResponseError(
      message: message,
      statusCode: statusCode,
      statusMessage: statusMessage,
      responseHeaders: responseHeaders,
      responseData: responseData,
      requestOptions: requestOptions,
      stackTrace: stackTrace,
      originalError: originalError,
      timestamp: timestamp,
    ),
  ParseError(:final source, :final rawSnippet, :final expectedType) =>
    ParseError(
      message: message,
      source: source,
      rawSnippet: rawSnippet,
      expectedType: expectedType,
      requestOptions: requestOptions,
      stackTrace: stackTrace,
      originalError: originalError,
      timestamp: timestamp,
    ),
  BusinessError(
    :final code,
    :final serverMessage,
    :final traceId,
    :final rawResponse,
  ) =>
    BusinessError(
      code: code,
      serverMessage: serverMessage,
      traceId: traceId,
      rawResponse: rawResponse,
      message: message,
      requestOptions: requestOptions,
      stackTrace: stackTrace,
      originalError: originalError,
      timestamp: timestamp,
    ),
  CancelError(:final cancelToken, :final reason) => CancelError(
    message: message,
    cancelToken: cancelToken,
    reason: reason,
    requestOptions: requestOptions,
    stackTrace: stackTrace,
    originalError: originalError,
    timestamp: timestamp,
  ),
  CertificateError(:final host, :final certificateInfo) => CertificateError(
    message: message,
    host: host,
    certificateInfo: certificateInfo,
    requestOptions: requestOptions,
    stackTrace: stackTrace,
    originalError: originalError,
    timestamp: timestamp,
  ),
  UnknownError() => UnknownError(
    message: message,
    requestOptions: requestOptions,
    stackTrace: stackTrace,
    originalError: originalError,
    timestamp: timestamp,
  ),
};