catching_josh 1.2.2
catching_josh: ^1.2.2 copied to clipboard
A Flutter package that makes try-catch operations elegant and customizable with clean error logging and flexible error handling options.
catching_josh #
Author: Joseph88
Version: 1.2.2
License: MIT
Git: https://github.com/joseph-seph88/catching_josh
Simple error handling with automatic logging - Focus on business logic, not error handling.
Installation #
dependencies:
catching_josh: ^1.2.2
Quick Start #
import 'package:catching_josh/catching_josh.dart';
// Sync operations
final result = joshSync(() => parseJson(jsonString));
// Async operations
final user = await joshAsync(() async => api.getUser(id));
// HTTP requests
final response = await joshReq(() async => http.get(url));
Core Functions #
| Function | Purpose | Return Type |
|---|---|---|
joshSync<T>() |
Sync operations | StandardResult |
joshAsync<T>() |
Async operations | Future<StandardResult> |
joshReq<T>() |
HTTP requests | Future<StandardResponse> |
Return Types #
class StandardResult {
final Object? data; // Result data
final String? dataType; // Data type
final String? errorMessage; // Error message
final bool? isSuccess; // Success status
}
class StandardResDataponse {
final int? statusCode; // HTTP status code
final String? statusMessage; // HTTP status message
final dynamic data; // Response data
final String? dataType; // Data type
final bool? isSuccess; // Success status
}
Examples #
Basic Usage #
// Sync operation
final result = joshSync(
() => jsonDecode(jsonString),
logTitle: 'JSON Parsing',
showErrorLog: true,
);
if (result.isSuccess == true) {
print('Data: ${result.data}');
}
HTTP Request #
final response = await joshReq(
() async => http.get(Uri.parse('https://api.example.com/data')),
mockResponseOnCatch: {'error': 'Network unavailable'}, // Mock data for testing
);
if (response.isSuccess == true) {
print('Status: ${response.statusCode}');
print('Data: ${response.data}');
}
Async Operation #
final user = await joshAsync(
() async => await api.getUser(userId),
logTitle: 'User Fetch',
showSuccessLog: true,
showErrorLog: true,
);
if (user.isSuccess == true) {
// Use user.data
}
Simple Log Line #
// Create a single formatted log line
final logLine = JoshLogger.singleLogLine('Something went wrong');
print(logLine); // Output: [ErrorMessage] Something went wrong
Features #
- ๐ฏ Purpose-specific methods:
joshSync,joshAsync,joshReq - ๐ Standardized returns: Always predictable result structure
- ๐ Automatic logging: Clean formatted logs with stack traces
- โก Dual logging system: User-facing + Internal batch logging
- ๐ก๏ธ Production-safe: Success logs disabled in production
- ๐งช Testing support: Mock data fallback for development environments
- ๐ Environment utils: Centralized environment detection
- ๐ Simple logging:
singleLogLine()for quick error message formatting - ๐ Zero dependencies: No external packages required
Parameters #
| Parameter | Type | Required | Description |
|---|---|---|---|
function |
Function |
โ | Function to execute |
logTitle |
String? |
โ | Title for log messages |
errorMessage |
String? |
โ | Custom error message |
showSuccessLog |
bool |
โ | Log success (default: false) |
showErrorLog |
bool |
โ | Log errors (default: false) |
mockResponseOnCatch |
dynamic |
โ | Mock data for testing (joshReq only) |
Environment Variables #
| Variable | Description | Default |
|---|---|---|
ENVIRONMENT |
Environment mode (dev/prod/production) | dev |
JOSH_LOGGER_MAX_CACHE_SIZE |
Log formatting cache size | 1000 |
Testing Support #
The joshReq function now supports mock data for testing:
// In development: returns mock data on error
// In production: returns null on error
final response = await joshReq(
() async => http.get(uri),
mockResponseOnCatch: {'test': 'data'},
);
License #
MIT License - see LICENSE file for details.