Skip to content

Commit

Permalink
Merge pull request #100 from nginformatica/feature/consider-value-error
Browse files Browse the repository at this point in the history
feat: consider value on error
  • Loading branch information
jacksjm authored Feb 7, 2024
2 parents aa422a8 + aea71ce commit 21a2a20
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 9 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "quirons-broker",
"version": "0.0.1-alpha.116",
"version": "0.0.1-alpha.117",
"description": "A small library to expose the broker types",
"main": "index.ts",
"typings": "index.d.ts",
Expand Down
17 changes: 13 additions & 4 deletions src/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ import { Either } from 'fp-ts/lib/Either'
import * as t from 'io-ts'
import { PathReporter } from 'io-ts/lib/PathReporter'

const Language = t.union([
export const Language = t.union([
t.literal('en-US'),
t.literal('pt-BR')
])
type Language = t.TypeOf<typeof Language>
export type Language = t.TypeOf<typeof Language>

// Recomendation of io-ts docs to create an union of literal strings
const ErrorCodeKey = t.keyof({
Expand Down Expand Up @@ -206,11 +206,16 @@ const raiseErrorFromDecode = <T>(
for (let index = 0; index < errors.length; index++) {
const error = errors[index]
const message = left.length >= index ? left[index].message : undefined
const value = left.length >= index ? left[index].value : undefined
// 'Invalid value undefined supplied to... /id: string'
// It will match 'id' and 'string'

if (message) {
attributes.push(message)
attributes.push(
value
? `${message} - ${value}`
: message
)
} else {
const [, attribute, type] =
error.match(/.+\/(.+): (.+)$/) || []
Expand All @@ -223,7 +228,11 @@ const raiseErrorFromDecode = <T>(
continue
}

attributes.push(`(${attribute}: ${type})`)
attributes.push(
value
? `(${attribute}: ${type}) - ${value}`
: `(${attribute}: ${type})`
)
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/esocial/messages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ export const Message = t.union([
])),
t.type({
// This should match the tag above
kind: t.literal('responseTAF'),
kind: t.literal('responseGov'),
errorMessage: t.string
})
])
Expand Down
9 changes: 6 additions & 3 deletions src/ttalk/internal/occupation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import * as t from 'io-ts'

import * as ttalk from '../'
import { cbo, datetime, nullable } from '../../custom-types'
import { raiseErrorFromDecode } from '../../errors'
import { raiseErrorFromDecode, Language } from '../../errors'

/**
* Our internal model for occupations.
Expand Down Expand Up @@ -31,11 +31,14 @@ export type Positions = t.TypeOf<typeof Positions>
* Standard message converter.
*/
export const Converter = {
fromTTalk(data: ttalk.PositionInfo): Positions {
fromTTalk(
data: ttalk.PositionInfo,
language: Language = 'en-US'
): Positions {
const result = ttalk.PositionInfo.decode(data)

if (result._tag === 'Left') {
throw raiseErrorFromDecode(result)
throw raiseErrorFromDecode(result, language)
}

return {
Expand Down

0 comments on commit 21a2a20

Please sign in to comment.