Skip to content

Refactor EdgeHitProcessor.processHit to clean up error handling#19

Merged
kevinlind merged 14 commits intoadobe:dev-v2.0.0from
kevinlind:mob-17882
Jan 25, 2023
Merged

Refactor EdgeHitProcessor.processHit to clean up error handling#19
kevinlind merged 14 commits intoadobe:dev-v2.0.0from
kevinlind:mob-17882

Conversation

@kevinlind
Copy link
Copy Markdown
Contributor

Description

Related Issue

Motivation and Context

How Has This Been Tested?

Screenshots (if appropriate):

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)

Checklist:

  • I have signed the Adobe Open Source CLA.
  • My code follows the code style of this project.
  • My change requires a change to the documentation.
  • I have updated the documentation accordingly.
  • I have read the CONTRIBUTING document.
  • I have added tests to cover my changes.
  • All new and existing tests passed.

@kevinlind kevinlind requested a review from emdobrin January 10, 2023 20:16
@codecov
Copy link
Copy Markdown

codecov bot commented Jan 10, 2023

Codecov Report

Merging #19 (e244810) into dev-v2.0.0 (31fe7e6) will increase coverage by 0.23%.
The diff coverage is 88.52%.

@@               Coverage Diff                @@
##             dev-v2.0.0      #19      +/-   ##
================================================
+ Coverage         83.17%   83.40%   +0.23%     
- Complexity          380      386       +6     
================================================
  Files                29       29              
  Lines              1545     1566      +21     
  Branches            219      221       +2     
================================================
+ Hits               1285     1306      +21     
  Misses              161      161              
  Partials             99       99              
Flag Coverage Δ
functional-tests 65.84% <60.66%> (-0.31%) ⬇️
unit-tests 79.31% <83.61%> (+0.09%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

Impacted Files Coverage Δ
...n/java/com/adobe/marketing/mobile/EdgeRequest.java 65.00% <57.14%> (-6.43%) ⬇️
...a/com/adobe/marketing/mobile/EdgeHitProcessor.java 85.59% <91.11%> (+3.78%) ⬆️
...e/marketing/mobile/CompletionCallbacksManager.java 96.88% <100.00%> (+0.45%) ⬆️
...com/adobe/marketing/mobile/EdgeNetworkService.java 73.82% <100.00%> (+0.42%) ⬆️
...adobe/marketing/mobile/NetworkResponseHandler.java 82.14% <100.00%> (ø)

Copy link
Copy Markdown
Contributor

@emdobrin emdobrin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks great, and thanks for splitting that method up 👍

@kevinlind kevinlind requested a review from emdobrin January 12, 2023 06:11
@kevinlind kevinlind requested review from addb and emdobrin January 20, 2023 20:46
// Tests that when a good hit is processed that a network request is made and the request returns 200

// setup
Map<String, Object> config = new HashMap<>();
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we add test cases with null config map?

final String responseStr =
"<RS>{\"some\":\"thing1\\n\"}<LF>" +
"<RS>{\"some\":\"thing2\\n\"}<LF>" +
"<RS>{\"some\":\"thing3\\n\"}<LF>."; // note the trailing '.' character
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we add comment saying "." is dropped with the exception?

while (scanner.hasNext()) {
final String jsonResult = scanner.next();
responseCallback.onResponse(jsonResult.substring(trimLength));
try {
Copy link
Copy Markdown
Contributor

@addb addb Jan 20, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it possible to avoid the exception all together by adding a check like this?

if (jsonResult.length() > trimlength) {
responseCallback.onResponse(jsonResult.substring(trimLength));
}

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I updated this to check the length instead of adding the try/catch block. If the jsonResult length is less than the record separator length, then I log a debug stating the response is ignored.

Comment on lines +99 to +108
StringWriter stringWriter = new StringWriter();
PrintWriter printWriter = new PrintWriter(stringWriter);
ex.printStackTrace(printWriter);

Log.warning(
LOG_TAG,
LOG_SOURCE,
"Exception thrown when invoking EdgeCallback.onComplete() for request event id %s: %s",
requestEventId,
stringWriter.toString()
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reading more about this it looks like it is recommended to use the platform level logging support for exceptions instead of printStackTrace.

@praveek any chance would could support for logging throwable in the Logging service as the platform provides?
https://developer.android.com/reference/android/util/Log#w(java.lang.String,%20java.lang.Throwable)

Alternatively we can look into using https://developer.android.com/reference/android/util/Log#getStackTraceString(java.lang.Throwable)

Copy link
Copy Markdown
Contributor

@praveek praveek Jan 24, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@emdobrin We can explore about adding these additional log methods after the 2.0 release. For now, I would prefer getStackTraceString to get the string representation of stack trace.

nit: Can we reword the log to Exception thrown when invoking completion callback for request event id
EdgeCallback.onComplete() is not very intuitive when a customer passes lambda.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@kevinlind can you make this update and also create a feature request for the logging service in aepsdk-core-android?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've updated the log message as suggested. I also created an issue in Android Core to add LoggingService APIs to support throwable objects, adobe/aepsdk-core-android#349

PrintWriter printWriter = new PrintWriter(stringWriter);
ex.printStackTrace(printWriter);

Log.warning(
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Log warning will look like the following, where an NPE is thrown from "throwUp" which is called from EdgeCallback.onComplete:

2023-01-20 10:16:15.788 16047-16192/com.adobe.marketing.edgetestapp W/AdobeExperienceSDK: Edge/CompletionCallbacksManager - Exception thrown when invoking EdgeCallback for request event id 00c61fbb-4c43-476e-9fd0-acc20a958b93: java.lang.NullPointerException: Catch me!
at com.adobe.marketing.tester.MainActivity.throwUp(MainActivity.java:239)
at com.adobe.marketing.tester.MainActivity.lambda$onSubmitSingleEvent$1$com-adobe-marketing-tester-MainActivity(MainActivity.java:232)
at com.adobe.marketing.tester.MainActivity$$ExternalSyntheticLambda4.onComplete(Unknown Source:6)
at com.adobe.marketing.mobile.CompletionCallbacksManager.unregisterCallback(CompletionCallbacksManager.java:97)
at com.adobe.marketing.mobile.EdgeHitProcessor$1.onComplete(EdgeHitProcessor.java:142)
at com.adobe.marketing.mobile.EdgeNetworkService.doRequest(EdgeNetworkService.java:230)
at com.adobe.marketing.mobile.EdgeHitProcessor.sendNetworkRequest(EdgeHitProcessor.java:175)
at com.adobe.marketing.mobile.EdgeHitProcessor.processExperienceEventHit(EdgeHitProcessor.java:267)
at com.adobe.marketing.mobile.EdgeHitProcessor.processHit(EdgeHitProcessor.java:97)
at com.adobe.marketing.mobile.services.PersistentHitQueue.lambda$processNextHit$2$com-adobe-marketing-mobile-services-PersistentHitQueue(PersistentHitQueue.java:118)
at com.adobe.marketing.mobile.services.PersistentHitQueue$$ExternalSyntheticLambda0.run(Unknown Source:2)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:463)
at java.util.concurrent.FutureTask.run(FutureTask.java:264)
at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:307)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1137)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:637)
at java.lang.Thread.run(Thread.java:1012)

while (scanner.hasNext()) {
final String jsonResult = scanner.next();
responseCallback.onResponse(jsonResult.substring(trimLength));
try {
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I updated this to check the length instead of adding the try/catch block. If the jsonResult length is less than the record separator length, then I log a debug stating the response is ignored.

Comment on lines +99 to +108
StringWriter stringWriter = new StringWriter();
PrintWriter printWriter = new PrintWriter(stringWriter);
ex.printStackTrace(printWriter);

Log.warning(
LOG_TAG,
LOG_SOURCE,
"Exception thrown when invoking EdgeCallback.onComplete() for request event id %s: %s",
requestEventId,
stringWriter.toString()
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've updated the log message as suggested. I also created an issue in Android Core to add LoggingService APIs to support throwable objects, adobe/aepsdk-core-android#349

@kevinlind kevinlind merged commit 8f074dc into adobe:dev-v2.0.0 Jan 25, 2023
@kevinlind kevinlind deleted the mob-17882 branch January 25, 2023 17:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants