FLUTTER ECOSYSTEM

erf/error_or

값 T 또는 오류 Object를 포함하는 ErrorOr 결과를 반환합니다.

오류 또는 프로젝트 이미지
Stars
11
Forks
0
최근 푸시(UTC)
2025. 7. 7.
프로젝트 상태
활성
Erlend GitHub avatar
GITHUB User

Erlend ↗

언어Dart

기술 주제

이 저장소가 배포한 패키지

사용 중인 의존성

의존성 목록 2 개
  • lints개발 의존성^6.0.0
  • test개발 의존성^1.26.2

원본 README

아래는 영문 원문 스냅샷입니다. 최신 내용은 GitHub에서 확인하세요.

README 펼치기 / 접기

ErrorOr

Return a result ErrorOr with either a value T or an error Object.

Features

Alternative error handling by wrapping a function that may throw exceptions, in a function that handle these and returns an ErrorOr.

A more explicit way of defining a function which may return an error and avoid having to try/catch these functions.

ErrorOr can be used for both async and synchronous functions.

API

Create an ErrorOr instance by calling one of its factory constructors value or error.

Check hasError or hasValue, before calling error or value. If either is called without the proper check, an ErrorOrTypeError is thrown.

The convenient function ErrorOr.wrap, does the try/catch logic for you given a throwing function. It returns a FutureOr so can be used for both async and synchronous functions.

Examples

Async example

Future<ErrorOr<T>> asyncFuncWhichThrowsWrapper() async {
  try {
    return ErrorOr.value(await asyncFuncWhichThrows());
  } catch (e) {
    return ErrorOr.error(e);
  }
}

ErrorOr<String> valueOrError = await asyncFuncWhichThrowsWrapper();
if (valueOrError.hasError) {
  return valueOrError;
}
String value = valueOrError.value;

Or let ErrorOr.wrap handle the try/catch logic

ErrorOr<String> valueOrError = await ErrorOr.wrap(asyncFuncWhichThrows)
if (valueOrError.hasError) {
  return valueOrError;
}
String value = valueOrError.value;

Additional information

See my article Error handling in Dart and ErrorOr.

The "ErrorOr" is inspired by the SerenityOS ErrorOr type.

The Success/Failure pattern was inspired by result_type.

I'd like to keep this package minimal, but please get in touch on github if you have suggestions to improvements.