Skip to content

Commit

Permalink
Merge pull request #1279 from OskarDamkjaer/ErrorTyping
Browse files Browse the repository at this point in the history
Properly type errors
  • Loading branch information
OskarDamkjaer authored Feb 11, 2021
2 parents 9caed5d + c1e051f commit 7f8487c
Show file tree
Hide file tree
Showing 10 changed files with 130 additions and 234 deletions.
5 changes: 2 additions & 3 deletions src/browser/modules/Stream/ErrorFrame.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
*/
import React from 'react'
import FrameTemplate from '../Frame/FrameTemplate'
import * as e from 'services/exceptionMessages'
import { createErrorObject, UnknownCommandError } from 'services/exceptions'
import { errorMessageFormater } from './errorMessageFormater'
import {
Expand All @@ -41,9 +40,9 @@ export const ErrorView = ({ frame }: any) => {
if (
!error.message &&
errorCode &&
typeof (e as any)[errorCode] !== 'undefined'
typeof createErrorObject(errorCode as any, error) !== 'undefined'
) {
const eObj = createErrorObject(errorCode, error)
const eObj = createErrorObject(errorCode as any, error)
errorContents = eObj.message
}
const fullError = errorMessageFormater(null, errorContents)
Expand Down
17 changes: 3 additions & 14 deletions src/shared/modules/commands/commandsDuck.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,7 @@ import configureMockStore, { MockStoreEnhanced } from 'redux-mock-store'
import { createEpicMiddleware } from 'redux-observable'
import { createBus, createReduxMiddleware } from 'suber'

import {
BoltConnectionError,
createErrorObject
} from '../../services/exceptions'
import { BoltConnectionError } from '../../services/exceptions'
import * as commands from './commandsDuck'
import helper from 'services/commandInterpreterHelper'
import { update as updateQueryResult } from '../requests/requestsDuck'
Expand Down Expand Up @@ -100,11 +97,7 @@ describe('commandsDuck', () => {
action,
send('cypher', requestId),
frames.add({ ...action, type: 'cypher' } as any),
updateQueryResult(
requestId,
createErrorObject(BoltConnectionError),
'error'
),
updateQueryResult(requestId, BoltConnectionError(), 'error'),
commands.unsuccessfulCypher(cmd),
fetchMetaData(),
{ type: 'NOOP' }
Expand Down Expand Up @@ -422,11 +415,7 @@ describe('commandsDuck', () => {
action,
send('cypher', requestId),
frames.add({ ...action, type: 'cypher' } as any),
updateQueryResult(
requestId,
createErrorObject(BoltConnectionError),
'error'
),
updateQueryResult(requestId, BoltConnectionError(), 'error'),
commands.unsuccessfulCypher(cmd),
fetchMetaData(),
{ type: 'NOOP' }
Expand Down
4 changes: 2 additions & 2 deletions src/shared/modules/commands/helpers/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
import { getCommandAndParam } from 'services/commandUtils'
import * as connections from 'shared/modules/connections/connectionsDuck'
import { add as addFrameAction } from 'shared/modules/stream/streamDuck'
import { UnknownCommandError, getErrorMessage } from 'services/exceptions'
import { UnknownCommandError } from 'services/exceptions'
import { shouldRetainConnectionCredentials } from 'shared/modules/dbMeta/dbMetaDuck'

export function handleServerCommand(action: any, put: any, store: any) {
Expand All @@ -48,7 +48,7 @@ export function handleServerCommand(action: any, put: any, store: any) {
return {
...action,
type: 'error',
error: { message: getErrorMessage(UnknownCommandError(action.cmd)) }
error: { message: UnknownCommandError(action.cmd).message }
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/shared/services/bolt/boltWorker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
*/
/* eslint-env serviceworker */
import 'core-js/stable'
import { BoltConnectionError, createErrorObject } from '../exceptions'
import { BoltConnectionError } from '../exceptions'
import {
ensureConnection,
closeConnection,
Expand Down Expand Up @@ -103,7 +103,7 @@ const onmessage = function(message: {
connectionProperties.opts,
() => {
;((self as unknown) as ServiceWorker).postMessage(
boltConnectionErrorMessage(createErrorObject(BoltConnectionError))
boltConnectionErrorMessage(BoltConnectionError())
)
}
)
Expand Down
5 changes: 4 additions & 1 deletion src/shared/services/bolt/boltWorkerMessages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,10 @@ export const postCancelTransactionMessage = (): Action => {
}
}

export const boltConnectionErrorMessage = (error: Error): AnyAction => {
export const boltConnectionErrorMessage = (error: {
code: string
message: string
}): AnyAction => {
return {
type: BOLT_CONNECTION_ERROR_MESSAGE,
error
Expand Down
6 changes: 3 additions & 3 deletions src/shared/services/bolt/transactions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import neo4j, { Session } from 'neo4j-driver'
import { v4 } from 'uuid'
import { getGlobalDrivers } from './globalDrivers'
import { buildTxFunctionByMode } from './boltHelpers'
import { BoltConnectionError, createErrorObject } from '../exceptions'
import { BoltConnectionError } from '../exceptions'

const runningQueryRegister: Record<string, (cb?: () => void) => void> = {}

Expand All @@ -36,7 +36,7 @@ function _trackedTransaction(
): [string, Promise<unknown>] {
const id = requestId || v4()
if (!session) {
return [id, Promise.reject(createErrorObject(BoltConnectionError))]
return [id, Promise.reject(BoltConnectionError())]
}
const closeFn = (cb = (): void => undefined): void => {
;(session.close as any)(cb)
Expand Down Expand Up @@ -88,7 +88,7 @@ function _transaction(
session: any,
txMetadata = undefined
): Promise<unknown> {
if (!session) return Promise.reject(createErrorObject(BoltConnectionError))
if (!session) return Promise.reject(BoltConnectionError())

const metadata = txMetadata ? { metadata: txMetadata } : undefined
const txFn = buildTxFunctionByMode(session)
Expand Down
53 changes: 12 additions & 41 deletions src/shared/services/commandInterpreterHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,12 +71,13 @@ import {
handleUpdateConfigCommand
} from 'shared/modules/commands/helpers/config'
import {
createErrorObject,
UnknownCommandError,
CouldNotFetchRemoteGuideError,
FetchURLError,
InvalidGrassError,
UnsupportedError
UnsupportedError,
DatabaseUnavailableError,
DatabaseNotFoundError
} from 'services/exceptions'
import {
parseHttpVerbCommand,
Expand Down Expand Up @@ -205,10 +206,7 @@ const availableCommands = [
try {
const supportsMultiDb = await bolt.hasMultiDbSupport()
if (!supportsMultiDb) {
throw createErrorObject(
UnsupportedError,
'No multi db support detected.'
)
throw UnsupportedError('No multi db support detected.')
}

const normalizedName = dbName.toLowerCase()
Expand All @@ -218,32 +216,12 @@ const availableCommands = [
(db: any) => db.name.toLowerCase() === cleanDbName
)

class UseDbError extends Error {
code: string
message: string

constructor(
{ code, message }: { code: string; message: string },
...params: any[]
) {
super(...params)
this.code = code
this.message = message
}
}

// Do we have a db with that name?
if (!dbMeta) {
throw new UseDbError({
code: 'NotFound',
message: `A database with the "${dbName}" name could not be found.`
})
throw DatabaseNotFoundError({ dbName })
}
if (dbMeta.status !== 'online') {
throw new UseDbError({
code: 'DatabaseUnavailable',
message: `Database "${dbName}" is unavailable, its status is "${dbMeta.status}."`
})
throw DatabaseUnavailableError({ dbName, dbMeta })
}
put(useDb(cleanDbName))
put(
Expand Down Expand Up @@ -295,10 +273,7 @@ const availableCommands = [
useDb: getUseDb(store.getState()),
...action,
type: 'error',
error: createErrorObject(
UnsupportedError,
'No multi db support detected.'
)
error: UnsupportedError('No multi db support detected.')
})
)
}
Expand All @@ -324,10 +299,7 @@ const availableCommands = [
useDb: getUseDb(store.getState()),
...action,
type: 'error',
error: createErrorObject(
UnsupportedError,
'No multi db support detected.'
)
error: UnsupportedError('No multi db support detected.')
})
)
}
Expand Down Expand Up @@ -599,7 +571,7 @@ const availableCommands = [
frames.add({
useDb: getUseDb(store.getState()),
...action,
error: createErrorObject(UnknownCommandError, action),
error: UnknownCommandError(action),
type: 'error'
})
)
Expand Down Expand Up @@ -716,8 +688,7 @@ const availableCommands = [
)
})
.catch(e => {
// @ts-expect-error ts-migrate(7009) FIXME: 'new' expression, whose target lacks a construct s... Remove this comment to see the full error message
const error = new FetchURLError({ error: e.message })
const error = FetchURLError({ error: e.message })
put(
frames.add({
useDb: getUseDb(store.getState()),
Expand Down Expand Up @@ -771,7 +742,7 @@ const availableCommands = [
frames.add({
useDb: getUseDb(store.getState()),
...action,
error: createErrorObject(InvalidGrassError, message),
error: InvalidGrassError(message),
type: 'error'
})
)
Expand Down Expand Up @@ -843,7 +814,7 @@ ${param}`)
frames.add({
useDb: getUseDb(store.getState()),
...action,
error: createErrorObject(UnknownCommandError, action),
error: UnknownCommandError(action),
type: 'error'
})
)
Expand Down
33 changes: 0 additions & 33 deletions src/shared/services/exceptionMessages.ts

This file was deleted.

72 changes: 0 additions & 72 deletions src/shared/services/exceptions.test.ts

This file was deleted.

Loading

0 comments on commit 7f8487c

Please sign in to comment.