Skip to content

Commit

Permalink
fix: ValidateMessage can throw an error
Browse files Browse the repository at this point in the history
Fixes: #10
  • Loading branch information
simonas-notcat committed Dec 19, 2019
1 parent 3db8186 commit d00dcdd
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 4 deletions.
4 changes: 2 additions & 2 deletions packages/daf-core/src/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { DIDDocument } from 'did-resolver'
import { IdentityManager, IdentityController } from './identity/identity-manager'
import { ServiceManager, LastMessageTimestampForInstance, ServiceEventTypes } from './service/service-manager'
import { ServiceControllerDerived } from './service/abstract-service-controller'
import { MessageValidator } from './message/message-validator'
import { MessageValidator, unsupportedMessageTypeError } from './message/abstract-message-validator'
import { ActionHandler } from './action/action-handler'
import { Action } from './types'
import { EncryptionKeyManager } from './encryption-manager'
Expand Down Expand Up @@ -99,7 +99,7 @@ export class Core extends EventEmitter {
this.emit(EventTypes.error, error, message)
return Promise.reject(error)
}
return Promise.reject('Unsupported message type')
return Promise.reject(unsupportedMessageTypeError)
}

public async handleAction(action: Action): Promise<any> {
Expand Down
2 changes: 1 addition & 1 deletion packages/daf-core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ export { Core, EventTypes, Resolver } from './core'
export { AbstractActionHandler } from './action/action-handler'
export { EncryptionKeyManager, KeyPair } from './encryption-manager'
export { IdentityController, IdentityManager, Issuer } from './identity/identity-manager'
export { AbstractMessageValidator } from './message/message-validator'
export { AbstractMessageValidator } from './message/abstract-message-validator'
export { Message } from './message/message'
export { ServiceManager, LastMessageTimestampForInstance, ServiceEventTypes } from './service/service-manager'
export { AbstractServiceController } from './service/abstract-service-controller'
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import { AbstractMessageValidator, unsupportedMessageTypeError } from '../abstract-message-validator'
import { Core } from '../../core'
import { Message } from '../message'

class MockMessageValidator extends AbstractMessageValidator {
async validate(message: Message, core: Core) {
if (message.raw === 'mock') {
message.type = 'mock'
return message
}
return super.validate(message, core)
}
}

class MockMessageValidatorWithError extends AbstractMessageValidator {
async validate(message: Message, core: Core) {
// This simulates a scenario when validation process encounters an error,
// such as a network error

throw Error('Network error')

return message
}
}

it('should return a promise and resolve it if the massage is of known type', async () => {
const msg = new Message({ raw: 'mock', meta: { type: 'test' } })
const validator = new MockMessageValidator()
const validated = await validator.validate(msg, null)
expect(validated.type).toEqual('mock')
expect(validated.isValid()).toEqual(true)
})

it('should return a promise and reject it if the massage is of unknown type', async () => {
const msg = new Message({ raw: 'unknown', meta: { type: 'test2' } })
const validator = new MockMessageValidator()
await expect(validator.validate(msg, null)).rejects.toEqual(unsupportedMessageTypeError)
})

it('can throw an error', async () => {
const msg = new Message({ raw: 'mock', meta: { type: 'test3' } })
const validator = new MockMessageValidatorWithError()
try {
const validated = await validator.validate(msg, null)
} catch (e) {
expect(e !== unsupportedMessageTypeError).toEqual(true)
}
})
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ export interface MessageValidator {
validate: (message: Message, core: Core) => Promise<Message>
}

export const unsupportedMessageTypeError = 'Unsupported message type'

export abstract class AbstractMessageValidator implements MessageValidator {
public nextMessageValidator?: MessageValidator

Expand All @@ -18,6 +20,6 @@ export abstract class AbstractMessageValidator implements MessageValidator {
if (this.nextMessageValidator) {
return this.nextMessageValidator.validate(message, core)
}
return Promise.reject('Unsupported message type')
return Promise.reject(unsupportedMessageTypeError)
}
}

0 comments on commit d00dcdd

Please sign in to comment.