error flattening, remain to details, and delete old config stuff

This commit is contained in:
Matt Hill
2021-08-09 13:39:30 -06:00
committed by Aiden McClelland
parent ae09f5a4f2
commit 6a9ec0ea05
26 changed files with 42 additions and 754 deletions

View File

@@ -2,6 +2,7 @@ import { Subject, Observable } from 'rxjs'
import { Http, Update, Operation, Revision, Source, Store } from 'patch-db-client'
import { RR } from '../api.types'
import { DataModel } from 'src/app/services/patch-db/data-model'
import { RequestError, RPCError } from '../../http.service'
export abstract class ApiService implements Source<DataModel>, Http<DataModel> {
protected readonly sync = new Subject<Update<DataModel>>()
@@ -189,10 +190,15 @@ export abstract class ApiService implements Source<DataModel>, Http<DataModel> {
// this.sync.next({ patch: [temp], expiredBy: expireId })
// }
return f(a).then(({ response, revision }) => {
if (revision) this.sync.next(revision)
return response
}) as any
return f(a)
.catch((e: RequestError) => {
if (e.revision) this.sync.next(e.revision)
throw e
})
.then(({ response, revision }) => {
if (revision) this.sync.next(revision)
return response
})
}
}
}

View File

@@ -19,9 +19,9 @@ export class ErrorToastService {
let message: string | IonicSafeString
if (e.status) message = String(e.status)
if (e.code) message = String(e.code)
if (e.message) message = `${message ? message + ' ' : ''}${e.message}`
if (e.data) message = `${message ? message + '. ' : ''}${e.data.code}: ${e.data.message}`
if (e.details) message = `${message ? message + ': ' : ''}${e.details}`
if (!message) {
message = 'Unknown Error.'

View File

@@ -16,9 +16,8 @@ export class HttpService {
private readonly http: HttpClient,
private readonly config: ConfigService,
) {
const { url, version } = this.config.api
const port = config.mocks.enabled ? this.config.mocks.rpcPort : window.location.port
this.fullUrl = `${window.location.protocol}//${window.location.hostname}:${port}/${url}/${version}`
this.fullUrl = `${window.location.protocol}//${window.location.hostname}:${port}`
}
watchUnauth$ (): Observable<{ }> {
@@ -26,11 +25,12 @@ export class HttpService {
}
async rpcRequest<T> (rpcOpts: RPCOptions): Promise<T> {
const { url, version } = this.config.api
rpcOpts.params = rpcOpts.params || { }
const httpOpts = {
method: Method.POST,
data: rpcOpts,
url: '',
url: `/${url}/${version}`,
}
const res = await this.httpRequest<RPCResponse<T>>(httpOpts)
@@ -91,21 +91,27 @@ export class HttpService {
}
function RpcError (e: RPCError['error']): void {
const { code, message } = e
this.status = code
const { code, message, data } = e
this.code = code
this.message = message
if (typeof e.data === 'string') {
throw new Error(`unexpected response for RPC Error data: ${e.data}`)
if (typeof data === 'string') {
this.details = e.data
this.revision = null
} else {
this.details = data.details
this.revision = data.revision
}
const data = e.data || { message: 'unknown RPC error', revision: null }
this.data = { ...data, code }
}
function HttpError (e: HttpErrorResponse): void {
const { status, statusText, error } = e
this.status = status
const { status, statusText } = e
this.code = status
this.message = statusText
this.data = error || { } // error = { code: string, message: string }
this.details = null
this.revision = null
}
function isRpcError<Error, Result> (arg: { error: Error } | { result: Result}): arg is { error: Error } {
@@ -117,9 +123,10 @@ function isRpcSuccess<Error, Result> (arg: { error: Error } | { result: Result})
}
export interface RequestError {
status: number
code: number
message: string
data: { code: string, message: string, revision: Revision | null }
details: string
revision: Revision | null
}
export enum Method {
@@ -157,7 +164,7 @@ export interface RPCError extends RPCBase {
code: number,
message: string
data?: {
message: string
details: string
revision: Revision | null
} | string
}

View File

@@ -1,24 +1,16 @@
import { Inject, Injectable, InjectionToken } from '@angular/core'
import { Injectable } from '@angular/core'
import { IonNav } from '@ionic/angular'
import { AppConfigComponentMapping } from '../modals/app-config-injectable'
import { ConfigCursor } from '../pkg-config/config-cursor'
export const APP_CONFIG_COMPONENT_MAPPING = new InjectionToken<string>('APP_CONFIG_COMPONENTS')
@Injectable({
providedIn: 'root',
})
export class SubNavService {
path: string[]
constructor (
@Inject(APP_CONFIG_COMPONENT_MAPPING) private readonly appConfigComponentMapping: AppConfigComponentMapping,
) { }
async push (key: string, cursor: ConfigCursor<any>, nav: IonNav) {
const component = this.appConfigComponentMapping[cursor.spec().type]
this.path.push(key)
nav.push(component, { cursor }, { mode: 'ios' })
// nav.push(component, { cursor }, { mode: 'ios' })
}
async pop (nav: IonNav) {