Skip to content

Commit

Permalink
Merge pull request #2 from klar-mx/add-errors-logging-to-alice
Browse files Browse the repository at this point in the history
Add errors logging to alice
  • Loading branch information
votruk authored Sep 27, 2023
2 parents 2df0a5d + 0b0d074 commit 2a58619
Show file tree
Hide file tree
Showing 11 changed files with 69 additions and 53 deletions.
4 changes: 2 additions & 2 deletions example/pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -262,10 +262,10 @@ packages:
dependency: transitive
description:
name: http
sha256: "759d1a329847dd0f39226c688d3e06a6b8679668e350e2891a6474f8b4bb8525"
sha256: "5895291c13fa8a3bd82e76d5627f69e0d85ca6a30dcac95c4ea19a5d555879c2"
url: "https://pub.dev"
source: hosted
version: "1.1.0"
version: "0.13.6"
http_multi_server:
dependency: transitive
description:
Expand Down
16 changes: 13 additions & 3 deletions lib/src/alice.dart
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import 'dart:io';

import 'package:alice/src/core/alice_core.dart';
import 'package:alice/src/core/alice_http_client_adapter.dart';
import 'package:alice/src/core/http/alice_http_adapter.dart';
import 'package:alice/src/core/http_client/alice_http_client_adapter.dart';
import 'package:alice/src/model/alice_http_call.dart';
import 'package:alice/src/model/alice_log.dart';
import 'package:flutter/material.dart';
Expand Down Expand Up @@ -61,8 +61,18 @@ class Alice {
}

/// Handle both request and response from http package
void onHttpResponse(http.Response response, {dynamic body}) {
_httpAdapter.onResponse(response, body: body);
void onHttpResponse(http.BaseResponse response, {dynamic body, Object? id}) {
_httpAdapter.onResponse(response, body: body, id: id);
}

/// Handle both request and response from http package
void onHttpRequest(http.BaseRequest request, {dynamic body, Object? id}) {
_httpAdapter.onRequest(request, body: body, id: id);
}

/// Handle both request and response from http package
void onHttpError(Object error, StackTrace? stackTrace, {required Object id}) {
_httpAdapter.onError(error, stackTrace, id: id);
}

/// Opens Http calls inspector. This will navigate user to the new fullscreen
Expand Down
19 changes: 7 additions & 12 deletions lib/src/core/alice_core.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import 'dart:async';

import 'package:alice/src/core/http_client/alice_logger.dart';
import 'package:alice/src/core/alice_logger.dart';
import 'package:alice/src/model/alice_http_call.dart';
import 'package:alice/src/model/alice_http_error.dart';
import 'package:alice/src/model/alice_http_response.dart';
Expand Down Expand Up @@ -84,20 +84,22 @@ class AliceCore {
}

/// Add error to existing alice http call
void addError(AliceHttpError error, int requestId) {
final AliceHttpCall? selectedCall = _selectCall(requestId);
void addError(AliceHttpError error, Object requestId) {
final selectedCall = _selectCall(requestId);

if (selectedCall == null) {
debugPrint("Selected call is null");
return;
}

selectedCall.error = error;
selectedCall.duration = error.time.millisecondsSinceEpoch -
selectedCall.request!.time.millisecondsSinceEpoch;
callsSubject.add([...callsSubject.value]);
}

/// Add response to existing alice http call
void addResponse(AliceHttpResponse response, int requestId) {
void addResponse(AliceHttpResponse response, Object requestId) {
final AliceHttpCall? selectedCall = _selectCall(requestId);

if (selectedCall == null) {
Expand All @@ -112,19 +114,12 @@ class AliceCore {
callsSubject.add([...callsSubject.value]);
}

/// Add alice http call to calls subject
void addHttpCall(AliceHttpCall aliceHttpCall) {
assert(aliceHttpCall.request != null, "Http call request can't be null");
assert(aliceHttpCall.response != null, "Http call response can't be null");
callsSubject.add([...callsSubject.value, aliceHttpCall]);
}

/// Remove all calls from calls subject
void removeCalls() {
callsSubject.add([]);
}

AliceHttpCall? _selectCall(int requestId) =>
AliceHttpCall? _selectCall(Object requestId) =>
callsSubject.value.firstWhereOrNull((call) => call.id == requestId);

/// Adds new log to Alice logger.
Expand Down
File renamed without changes.
54 changes: 30 additions & 24 deletions lib/src/core/http/alice_http_adapter.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import 'dart:convert';

import 'package:alice/src/core/alice_core.dart';
import 'package:alice/src/model/alice_http_call.dart';
import 'package:alice/src/model/alice_http_error.dart';
import 'package:alice/src/model/alice_http_request.dart';
import 'package:alice/src/model/alice_http_response.dart';
import 'package:http/http.dart' as http;
Expand All @@ -13,14 +14,8 @@ class AliceHttpAdapter {
/// Creates alice http adapter
AliceHttpAdapter(this.aliceCore);

/// Handles http response. It creates both request and response from http call
void onResponse(http.Response response, {dynamic body}) {
if (response.request == null) {
return;
}
final request = response.request!;

final AliceHttpCall call = AliceHttpCall(response.request.hashCode);
void onRequest(http.BaseRequest request, {dynamic body, Object? id}) {
final AliceHttpCall call = AliceHttpCall(id ?? request.hashCode);
call.loading = true;
call.client = "HttpClient (http package)";
call.uri = request.url.toString();
Expand All @@ -38,16 +33,15 @@ class AliceHttpAdapter {

final AliceHttpRequest httpRequest = AliceHttpRequest();

if (response.request is http.Request) {
if (request is http.Request) {
// we are guaranteed` the existence of body and headers
if (body != null) {
httpRequest.body = body;
}
// ignore: cast_nullable_to_non_nullable
httpRequest.body = body ?? (response.request as http.Request).body ?? "";
httpRequest.body = body ?? request.body ?? "";
httpRequest.size = utf8.encode(httpRequest.body.toString()).length;
httpRequest.headers =
Map<String, dynamic>.from(response.request!.headers);
httpRequest.headers = Map<String, dynamic>.from(request.headers);
} else if (body == null) {
httpRequest.size = 0;
httpRequest.body = "";
Expand All @@ -65,26 +59,38 @@ class AliceHttpAdapter {

httpRequest.contentType = contentType;

httpRequest.queryParameters = response.request!.url.queryParameters;
httpRequest.queryParameters = request.url.queryParameters;
call.request = httpRequest;
aliceCore.addCall(call);
}

final AliceHttpResponse httpResponse = AliceHttpResponse();
/// Handles http response. It creates both request and response from http call
void onResponse(http.BaseResponse response, {dynamic body, Object? id}) {
final httpResponse = AliceHttpResponse();
httpResponse.status = response.statusCode;
httpResponse.body = response.body;

httpResponse.size = utf8.encode(response.body.toString()).length;
if (response is http.Response) {
httpResponse.body = body ?? response.body;
httpResponse.size =
utf8.encode((body ?? response.body).toString()).length;
} else {
httpResponse.body = body ?? '';
httpResponse.size = utf8.encode((body ?? '').toString()).length;
}
httpResponse.time = DateTime.now();
final Map<String, String> responseHeaders = {};
response.headers.forEach((header, values) {
responseHeaders[header] = values.toString();
});
httpResponse.headers = responseHeaders;
aliceCore.addResponse(httpResponse, id ?? response.hashCode);
}

call.request = httpRequest;
call.response = httpResponse;

call.loading = false;
call.duration = httpResponse.time.millisecondsSinceEpoch -
httpRequest.time.millisecondsSinceEpoch;
aliceCore.addCall(call);
void onError(Object error, StackTrace? stackTrace, {required Object id}) {
final httpError = AliceHttpError(
error: error,
stackTrace: stackTrace,
time: DateTime.now(),
);
aliceCore.addError(httpError, id);
}
}
File renamed without changes.
7 changes: 1 addition & 6 deletions lib/src/model/alice_http_call.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import 'package:alice/src/model/alice_http_request.dart';
import 'package:alice/src/model/alice_http_response.dart';

class AliceHttpCall {
final int id;
final Object id;
late DateTime createdTime;
String client = "";
bool loading = true;
Expand All @@ -23,11 +23,6 @@ class AliceHttpCall {
createdTime = DateTime.now();
}

void setResponse(AliceHttpResponse response) {
this.response = response;
loading = false;
}

String getCurlCommand() {
var compressed = false;
var curlCmd = "curl";
Expand Down
14 changes: 12 additions & 2 deletions lib/src/model/alice_http_error.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,14 @@
import 'package:meta/meta.dart';

@immutable
class AliceHttpError {
dynamic error;
StackTrace? stackTrace;
final Object error;
final StackTrace? stackTrace;
final DateTime time;

AliceHttpError({
required this.error,
required this.stackTrace,
required this.time,
});
}
2 changes: 1 addition & 1 deletion lib/src/ui/page/alice_calls_list_screen.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import 'package:alice/src/core/alice_core.dart';
import 'package:alice/src/core/http_client/alice_logger.dart';
import 'package:alice/src/core/alice_logger.dart';
import 'package:alice/src/helper/alice_alert_helper.dart';
import 'package:alice/src/model/alice_http_call.dart';
import 'package:alice/src/model/alice_menu_item.dart';
Expand Down
4 changes: 2 additions & 2 deletions pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -79,10 +79,10 @@ packages:
dependency: "direct main"
description:
name: http
sha256: "759d1a329847dd0f39226c688d3e06a6b8679668e350e2891a6474f8b4bb8525"
sha256: "5895291c13fa8a3bd82e76d5627f69e0d85ca6a30dcac95c4ea19a5d555879c2"
url: "https://pub.dev"
source: hosted
version: "1.1.0"
version: "0.13.6"
http_parser:
dependency: transitive
description:
Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ environment:
dependencies:
flutter:
sdk: flutter
http: ^1.1.0
http: ^0.13.0
rxdart: ^0.27.7

package_info_plus: ^4.0.2
Expand Down

0 comments on commit 2a58619

Please sign in to comment.