-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5 from natsuk4ze/Add-CompassXException
Add CompassXException
- Loading branch information
Showing
5 changed files
with
69 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import 'package:flutter/foundation.dart'; | ||
import 'package:flutter/services.dart'; | ||
|
||
@immutable | ||
final class CompassXException implements Exception { | ||
const CompassXException._({ | ||
required this.type, | ||
required this.platformException, | ||
required this.stackTrace, | ||
}); | ||
|
||
final CompassXExceptionType type; | ||
final PlatformException platformException; | ||
final StackTrace stackTrace; | ||
|
||
factory CompassXException.fromPlatformException({ | ||
required PlatformException platformException, | ||
required StackTrace stackTrace, | ||
}) => | ||
CompassXException._( | ||
type: CompassXExceptionType.values.firstWhere( | ||
(type) => type.code == platformException.code, | ||
orElse: () => CompassXExceptionType.unexpected, | ||
), | ||
platformException: platformException, | ||
stackTrace: stackTrace, | ||
); | ||
|
||
@override | ||
String toString() => "[CompassXException/${type.code}]: ${type.message}"; | ||
} | ||
|
||
enum CompassXExceptionType { | ||
/// This is used for older or excessively cheap Android devices | ||
/// that do not have a compass sensor. | ||
sensorNotFound, | ||
|
||
/// This is used when an unexpected error occurs. | ||
/// See [PlatformException] for more information. | ||
unexpected; | ||
|
||
String get code => switch (this) { | ||
sensorNotFound => 'SENSOR_NOT_FOUND', | ||
unexpected => 'UNEXPECTED', | ||
}; | ||
|
||
String get message => switch (this) { | ||
sensorNotFound => 'Compass sensor not found.', | ||
unexpected => 'An unexpected error has occurred.', | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters