Skip to content

Commit

Permalink
Capture http errors android (#385)
Browse files Browse the repository at this point in the history
* Capture HTTP errors on Anrdoid

* Lint

* Inclode code

---------

Co-authored-by: KoalaSat <yv1vtrul@duck.com>
  • Loading branch information
Reckless-Satoshi and KoalaSat authored Mar 9, 2023
1 parent 57a47a4 commit 06558f1
Showing 1 changed file with 22 additions and 5 deletions.
27 changes: 22 additions & 5 deletions mobile/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,23 @@ const App = () => {
loadCookie('garage').then(() => injectMessageResolve(reponseId));
};

const onCatch = (dataId: string, event: any) => {
let json = '{}';
let code = 500;
if (event.message) {
const reponse = /Request Response Code \((?<code>\d*)\)\: (?<json>\{.*\})/.exec(
event.message,
);
json = reponse?.groups?.json ?? '{}';
code = reponse?.groups?.code ? parseInt(reponse?.groups?.code) : 500;
}
injectMessageResolve(dataId, {
headers: {},
respCode: code,
json: JSON.parse(json),
});
};

const onMessage = async (event: WebViewMessageEvent) => {
const data = JSON.parse(event.nativeEvent.data);
if (data.category === 'http') {
Expand All @@ -63,31 +80,31 @@ const App = () => {
.then((response: object) => {
injectMessageResolve(data.id, response);
})
.catch(sendTorStatus)
.catch((e) => onCatch(data.id, e))
.finally(sendTorStatus);
} else if (data.type === 'post') {
torClient
.post(data.baseUrl, data.path, data.body, data.headers)
.then((response: object) => {
injectMessageResolve(data.id, response);
})
.catch(sendTorStatus)
.catch((e) => onCatch(data.id, e))
.finally(sendTorStatus);
} else if (data.type === 'delete') {
torClient
.delete(data.baseUrl, data.path, data.headers)
.then((response: object) => {
injectMessageResolve(data.id, response);
})
.catch(sendTorStatus)
.catch((e) => onCatch(data.id, e))
.finally(sendTorStatus);
} else if (data.type === 'xhr') {
torClient
.request(data.baseUrl, data.path)
.then((response: object) => {
injectMessageResolve(data.id, response);
})
.catch(sendTorStatus)
.catch((e) => onCatch(data.id, e))
.finally(sendTorStatus);
}
} else if (data.category === 'system') {
Expand Down Expand Up @@ -116,7 +133,7 @@ const App = () => {
} catch (error) {}
};

const sendTorStatus = async () => {
const sendTorStatus = async (event?: any) => {
NetInfo.fetch().then(async (state) => {
let daemonStatus = 'ERROR';
if (state.isInternetReachable) {
Expand Down

0 comments on commit 06558f1

Please sign in to comment.