catching_josh 1.2.2 copy "catching_josh: ^1.2.2" to clipboard
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.

pub package License


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.

1
likes
0
points
207
downloads

Publisher

unverified uploader

Weekly Downloads

A Flutter package that makes try-catch operations elegant and customizable with clean error logging and flexible error handling options.

Repository (GitHub)
View/report issues

License

unknown (license)

Dependencies

flutter

More

Packages that depend on catching_josh