Skip to content

Commit

Permalink
fix: offline more bugs (#896)
Browse files Browse the repository at this point in the history
* fix: app offline mode bugs

* fix: add other errors and gateway timeout + refactor

* feat(ui): don't display generic error if from offline mode
  • Loading branch information
iFergal authored Jan 8, 2025
1 parent d825ff8 commit 3556722
Show file tree
Hide file tree
Showing 9 changed files with 89 additions and 64 deletions.
4 changes: 1 addition & 3 deletions ios/App/App/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
<true/>
</dict>
<key>NSCameraUsageDescription</key>
<string>To be able to scan barcodes</string>
<string>The app enables the scanning of various barcodes.</string>
<key>NSFaceIDUsageDescription</key>
<string>This app uses Face ID to secure your data and provide faster access.</string>
<key>UIFileSharingEnabled</key>
Expand Down Expand Up @@ -78,7 +78,5 @@
</dict>
</dict>
</array>
<key>NSCameraUsageDescription</key>
<string>The app enables the scanning of various barcodes.</string>
</dict>
</plist>
2 changes: 1 addition & 1 deletion ios/App/Podfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -205,4 +205,4 @@ SPEC CHECKSUMS:

PODFILE CHECKSUM: 3d984c226b41598bd9ba6e349fdb4a7ae11e20f7

COCOAPODS: 1.14.3
COCOAPODS: 1.15.2
36 changes: 29 additions & 7 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

24 changes: 10 additions & 14 deletions src/core/agent/agent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ import { BaseRecord } from "../storage/storage.types";
import { OperationPendingStorage } from "./records/operationPendingStorage";
import { OperationPendingRecord } from "./records/operationPendingRecord";
import { EventTypes, KeriaStatusChangedEvent } from "./event.types";
import { isNetworkError } from "./services/utils";

const walletId = "idw";
class Agent {
Expand Down Expand Up @@ -181,9 +182,9 @@ class Agent {
this.multiSigs,
this.ipexCommunications,
this.credentialService,
this.getKeriaOnlineStatus,
this.markAgentStatus,
this.connect
this.getKeriaOnlineStatus.bind(this),
this.markAgentStatus.bind(this),
this.connect.bind(this)
);
}
return this.keriaNotificationService;
Expand Down Expand Up @@ -329,19 +330,20 @@ class Agent {
if (!(error instanceof Error)) {
throw error;
}
/* eslint-disable no-console */
console.error(error);
const status = error.message.split(" - ")[1];
if (error.message === "Failed to fetch") {

if (isNetworkError(error)) {
throw new Error(Agent.KERIA_CONNECT_FAILED_BAD_NETWORK, {
cause: error,
});
}

const status = error.message.split(" - ")[1];
if (/404/gi.test(status)) {
throw new Error(Agent.KERIA_NOT_BOOTED, {
cause: error,
});
}

throw new Error(Agent.KERIA_BOOTED_ALREADY_BUT_CANNOT_CONNECT, {
cause: error,
});
Expand Down Expand Up @@ -485,13 +487,7 @@ class Agent {
});
}
await this.signifyClient.connect();
Agent.isOnline = true;
this.agentServicesProps.eventEmitter.emit<KeriaStatusChangedEvent>({
type: EventTypes.KeriaStatusChanged,
payload: {
isOnline: true,
},
});
this.markAgentStatus(true);
} catch (error) {
await new Promise((resolve) => setTimeout(resolve, retryInterval));
await this.connect(retryInterval);
Expand Down
24 changes: 12 additions & 12 deletions src/core/agent/services/keriaNotificationService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ import {
NotificationRemovedEvent,
ConnectionStateChangedEvent,
} from "../event.types";
import { deleteNotificationRecordById, randomSalt } from "./utils";
import { deleteNotificationRecordById, isNetworkError, randomSalt } from "./utils";
import { CredentialService } from "./credentialService";
import { ConnectionHistoryType, ExnMessage } from "./connectionService.types";
import { NotificationAttempts } from "../records/notificationRecord.types";
Expand Down Expand Up @@ -150,13 +150,13 @@ class KeriaNotificationService extends AgentService {
.notifications()
.list(startFetchingIndex, startFetchingIndex + 24);
} catch (error) {
const errorMessage = (error as Error).message;
/** If the error is failed to fetch with signify,
* we retry until the connection is secured*/
if (!(error instanceof Error)) {
throw error;
}

if (
(/Failed to fetch/gi.test(errorMessage) ||
/Load failed/gi.test(errorMessage)) &&
this.getKeriaOnlineStatus()
this.getKeriaOnlineStatus() &&
isNetworkError(error)
) {
// Possible that bootAndConnect is called from @OnlineOnly in between loops,
// so check if its gone down to avoid having 2 bootAndConnect loops
Expand Down Expand Up @@ -967,12 +967,12 @@ class KeriaNotificationService extends AgentService {
.operations()
.get(operationRecord.id);
} catch (error) {
const errorMessage = (error as Error).message;
/** If the error is failed to fetch with signify,
* we retry until the connection is secured*/
if (!(error instanceof Error)) {
throw error;
}

if (
(/Failed to fetch/gi.test(errorMessage) ||
/Load failed/gi.test(errorMessage)) &&
isNetworkError(error) &&
this.getKeriaOnlineStatus()
) {
this.markAgentStatus(false);
Expand Down
39 changes: 25 additions & 14 deletions src/core/agent/services/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,28 +33,25 @@ function getCredentialShortDetails(
}

export const OnlineOnly = (
target: any,
propertyKey: string,
_target: any,
_propertyKey: string,
descriptor: PropertyDescriptor
) => {
const originalMethod = descriptor.value;
descriptor.value = async function (...args: any[]) {
const isKeriOnline = Agent.agent.getKeriaOnlineStatus();
if (!isKeriOnline) {
if (!Agent.agent.getKeriaOnlineStatus()) {
throw new Error(Agent.KERIA_CONNECTION_BROKEN);
}
// Call the original method
try {
const executeResult = await originalMethod.apply(this, args);
return executeResult;
} catch (error) {
const errorMessage = (error as Error).message;
/** If the error is failed to fetch with signify,
* we retry until the connection is secured*/
if (
/Failed to fetch/gi.test(errorMessage) ||
/Load failed/gi.test(errorMessage)
) {
if (!(error instanceof Error)) {
throw error;
}

if (isNetworkError(error)) {
Agent.agent.markAgentStatus(false);
Agent.agent.connect(1000);
throw new Error(Agent.KERIA_CONNECTION_BROKEN, {
Expand Down Expand Up @@ -86,8 +83,22 @@ export const deleteNotificationRecordById = async (
};

function randomSalt(): string {
const salt = new Salter({}).qb64;
return salt;
return new Salter({}).qb64;
}

function isNetworkError(error: Error): boolean {
if (
/Failed to fetch/gi.test(error.message) ||
/network error/gi.test(error.message) ||
/Load failed/gi.test(error.message) ||
/NetworkError when attempting to fetch resource./gi.test(error.message) ||
/The Internet connection appears to be offline./gi.test(error.message) ||
/504/gi.test(error.message.split(" - ")[1]) // Gateway timeout
) {
return true;
}

return false;
}

export { waitAndGetDoneOp, getCredentialShortDetails, randomSalt };
export { waitAndGetDoneOp, getCredentialShortDetails, randomSalt, isNetworkError };
Loading

0 comments on commit 3556722

Please sign in to comment.