allow falsey rpc response (#1680)

* allow falsey rpc response

* better check for rpc error and remove extra function

Co-authored-by: Matt Hill <matthewonthemoon@gmail.com>
This commit is contained in:
Aiden McClelland
2022-07-25 10:16:04 -06:00
committed by GitHub
parent 36c720227f
commit 908a945b95
4 changed files with 8 additions and 29 deletions

View File

@@ -11,9 +11,7 @@ export class HttpService {
async rpcRequest<T>(options: RPCOptions): Promise<T> {
const res = await this.httpRequest<RPCResponse<T>>(options)
if (isRpcError(res)) throw new RpcError(res.error)
if (isRpcSuccess(res)) return res.result
throw new Error('Unknown RPC response')
return res.result
}
async httpRequest<T>(body: RPCOptions): Promise<T> {
@@ -31,13 +29,7 @@ export class HttpService {
function isRpcError<Error, Result>(
arg: { error: Error } | { result: Result },
): arg is { error: Error } {
return !!(arg as any).error
}
function isRpcSuccess<Error, Result>(
arg: { error: Error } | { result: Result },
): arg is { result: Result } {
return !!(arg as any).result
return (arg as any).error !== undefined
}
export interface RPCOptions {

View File

@@ -42,9 +42,7 @@ export class HttpService {
throw new RpcError(res.error)
}
if (isRpcSuccess(res)) return res.result
throw new Error('Unknown RPC response')
return res.result
}
async encryptedHttpRequest<T>(httpOpts: {
@@ -132,13 +130,7 @@ class EncryptionError {
function isRpcError<Error, Result>(
arg: { error: Error } | { result: Result },
): arg is { error: Error } {
return !!(arg as any).error
}
function isRpcSuccess<Error, Result>(
arg: { error: Error } | { result: Result },
): arg is { result: Result } {
return !!(arg as any).result
return (arg as any).error !== undefined
}
export enum Method {

View File

@@ -170,6 +170,7 @@ export class AppConfigPage {
} catch (e: any) {
this.errToast.present(e)
this.saving = false
loader.dismiss()
}
}

View File

@@ -1,6 +1,6 @@
import { Injectable } from '@angular/core'
import { HttpClient, HttpHeaders, HttpParams } from '@angular/common/http'
import { Observable, from, interval, race } from 'rxjs'
import { from, interval, Observable, race } from 'rxjs'
import { map, take } from 'rxjs/operators'
import { ConfigService } from './config.service'
import { Revision } from 'patch-db-client'
@@ -40,7 +40,7 @@ export class HttpService {
throw new RpcError(res.error)
}
if (isRpcSuccess(res)) return res.result
return res.result
}
async httpRequest<T>(httpOpts: HttpOptions): Promise<T> {
@@ -102,13 +102,7 @@ export class HttpService {
function isRpcError<Error, Result>(
arg: { error: Error } | { result: Result },
): arg is { error: Error } {
return !!(arg as any).error
}
function isRpcSuccess<Error, Result>(
arg: { error: Error } | { result: Result },
): arg is { result: Result } {
return !!(arg as any).result
return (arg as any).error !== undefined
}
export interface RequestError {