Skip to content

Commit

Permalink
Improve image could not be uploaded internal server error handling
Browse files Browse the repository at this point in the history
Signed-off-by: Dmytro Turskyi <[email protected]>
  • Loading branch information
Turskyi committed Feb 13, 2024
1 parent 7ca7e4d commit a7eac69
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 8 deletions.
1 change: 1 addition & 0 deletions components/core/entities/lib/entities.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export 'src/enums/language.dart';
export 'src/enums/product_info_type.dart';
export 'src/enums/vegan.dart';
export 'src/enums/vegetarian.dart';
export 'src/exception/internal_server_error.dart';
export 'src/exception/not_found_exception.dart';
export 'src/logging_interceptor/logging_interceptor.dart';
export 'src/product/product_info.dart';
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
class InternalServerError implements Exception {
const InternalServerError(this.message);

final String message;

@override
String toString() => message;
}
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,19 @@ class _CameraScreenState extends State<PhotoView> {
),
),
if (viewModel is AddIngredientsErrorState)
Text(viewModel.errorMessage),
Container(
padding: const EdgeInsets.all(20),
alignment: Alignment.center,
child: Text(
viewModel.errorMessage,
textAlign: TextAlign.center,
style: TextStyle(
color: Colors.white,
fontWeight: FontWeight.bold,
fontSize: Theme.of(context).textTheme.bodyLarge?.fontSize,
),
),
),
],
);
},
Expand Down Expand Up @@ -219,13 +231,12 @@ class _CameraScreenState extends State<PhotoView> {
}
} catch (e) {
debugPrint('Error taking picture: $e');
} finally {
// TODO: should we await _controller.dispose?
}
},
child: viewModel is TakenPhotoState
? const Icon(Icons.send)
: viewModel is PhotoMakerReadyState
: viewModel is PhotoMakerReadyState ||
viewModel is AddIngredientsErrorState
? const Icon(Icons.camera)
: viewModel is LoadingState
? const Icon(Icons.stop)
Expand Down
12 changes: 8 additions & 4 deletions lib/data/data_sources/remote/remote_data_source_impl.dart
Original file line number Diff line number Diff line change
Expand Up @@ -168,11 +168,15 @@ class RemoteDataSourceImpl implements RemoteDataSource {
),
image,
);

if (status.status != 'status ok') {
if (status.status == HttpStatus.internalServerError) {
throw InternalServerError(
'Image could not be uploaded: ${status.error}.\n'
'${status.imageId != null ? status.imageId.toString() : ''}',
);
} else if (status.status != 'status ok') {
throw Exception(
'image could not be uploaded: ${status.error} '
'${status.imageId.toString()}',
'image could not be uploaded: ${status.error}.\n'
'${status.imageId != null ? status.imageId.toString() : ''}',
);
}
}
Expand Down

0 comments on commit a7eac69

Please sign in to comment.