Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: introduce default mapping strategy #14

Merged
merged 3 commits into from
Nov 18, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,23 @@ import { StatusCodeErrorMapper } from 'http-problem-details-mapper'
const problem = StatusCodeErrorMapper.mapStatusCode(400)
```

Similar to the `DefaultErrorMapper` there's also a `DefaultMappingStrategy` which you can use if you have no specific requirements regarding the mapping behavior.

It can be used like this:

```js
import { MapperRegistry, DefaultMappingStrategy } from 'http-problem-details-mapper'

const strategy = new DefaultMappingStrategy(
new MapperRegistry()
.registerMapper(new NotFoundErrorMapper()))

const error = new NotFoundError({ type: 'customer', id: '123' })
const problem = strategy.map()

console.log(problem)
```

## Running the tests

```
Expand Down
21 changes: 21 additions & 0 deletions src/DefaultMappingStrategy.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { ProblemDocument } from 'http-problem-details'
import { IMappingStrategy } from './IMappingStrategy'
import { MapperRegistry } from './MapperRegistry'

export class DefaultMappingStrategy implements IMappingStrategy {
public readonly registry: MapperRegistry

public constructor (registry: MapperRegistry) {
this.registry = registry
}

public map (error: any): ProblemDocument | null {
const err = error
const errorMapper = this.registry.getMapper(error)
if (errorMapper) {
return errorMapper.mapError(err)
}

return null
}
}
2 changes: 2 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@ import { DefaultErrorMapper } from './DefaultErrorMapper'
import { StatusCodeErrorMapper } from './StatusCodeErrorMapper'
import { IMappingStrategy, MappingStrategy } from './IMappingStrategy'
import { ErrorStatusCodes } from './ErrorStatusCodes'
import { DefaultMappingStrategy } from './DefaultMappingStrategy'

export {
DefaultMappingStrategy,
ErrorStatusCodes,
ErrorMapper,
IErrorMapper,
Expand Down
14 changes: 14 additions & 0 deletions test/DefaultMappingStrategyTests.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { MapperRegistry, DefaultMappingStrategy } from '../src'
import 'should'

describe('DefaultMappingStrategy', (): void => {
describe('When mapping a generic error', (): void => {
it('should return status code 500 problem', (done): void => {
const strategy = new DefaultMappingStrategy(new MapperRegistry())
const problem = strategy.map(new Error())!
problem.should.have.property('type', 'about:blank')
problem.status.should.equal(500)
return done()
})
})
})