Skip to content

Commit

Permalink
Merge pull request #2 from rathod-sahaab/feat/guess-error-kind-example
Browse files Browse the repository at this point in the history
Feat/guess error kind example
  • Loading branch information
rathod-sahaab authored Nov 13, 2024
2 parents 8d4d322 + 9da52e0 commit 04b14e1
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 16 deletions.
24 changes: 23 additions & 1 deletion examples/live/service.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
import { HttpErrorResults, Ok, Result, Err } from '../../src/index'
import {
HttpErrorResults,
Ok,
Result,
Err,
IHttpErrorKind,
} from '../../src/index'
import { Post } from './contract'

export function serviceCreatePost(
Expand All @@ -14,3 +20,19 @@ export function serviceCreatePost(

return Ok({ article })
}

export const serviceCreatePostAutoErrorKind = ((article: string) => {
// Automatically deduce return error kinds
// I don't like it Result is broken up into union components
if (article.length < 10) {
return Err('BadRequest', 'Article too small')
}

if (article.length > 100) {
return HttpErrorResults.InternalServerError('Error storing in DB')
}

const post: Post = { article }

return Ok(post)
}) satisfies (article: string) => Result<Post, IHttpErrorKind>
2 changes: 1 addition & 1 deletion jsr.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@rathod-sahaab/http-result",
"version": "0.5.2",
"version": "0.5.3",
"exports": {
".": "./src/index.ts"
"./ts-rest": "./src/ts-rest/index.ts"
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
"error-handling"
],
"private": false,
"version": "0.5.2",
"version": "0.5.3",
"type": "module",
"main": "./dist/http-result.umd.cjs",
"module": "./dist/http-result.js",
Expand Down
26 changes: 13 additions & 13 deletions src/functions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

import { HTTP_ERRORS } from './http.constants'
import { IHttpErrorKind } from './http.types'
import { ErrPayload, ErrType, Result, Success } from './types'
import { ErrPayload, Result, Success } from './types'

/**
* Helper function to easily create result success type from object
Expand All @@ -24,28 +24,29 @@ export function Ok<T>(value: T): Success<T> {
* return Err('BadRequest', 'Article Too long.')
* ```
*/
export function Err<E extends IHttpErrorKind>(
export function Err<T extends any, E extends IHttpErrorKind>(
kind: E,
message: string,
): ErrType<E> {
): Result<T, E> {
return [
null,
{
status: HTTP_ERRORS[kind],
kind,
messages: [message],
},
]
] as Result<T, E>
}

const errorFactory = <K extends IHttpErrorKind, A extends IHttpErrorKind>(
kind: K,
): ((message: string, baseError: ErrPayload<A>) => ErrPayload<K>) => {
return (message: string, baseError?: ErrPayload<A>) => ({
status: HTTP_ERRORS[kind],
kind: kind,
messages: baseError ? [...baseError.messages, message] : [message],
})
return (message: string, baseError?: ErrPayload<A>) =>
({
status: HTTP_ERRORS[kind],
kind: kind,
messages: baseError ? [...baseError.messages, message] : [message],
}) as ErrPayload<K>
}

const errorResultFactory = <
Expand All @@ -55,10 +56,9 @@ const errorResultFactory = <
>(
errorMaker: (message: string, baseError?: ErrPayload<A>) => ErrPayload<E>,
): ((message: string, baseError?: ErrPayload<A>) => Result<T, E>) => {
return (message: string, baseError?: ErrPayload<A>) => [
null,
errorMaker(message, baseError),
]
return (message: string, baseError?: ErrPayload<A>): Result<T, E> => {
return [null, errorMaker(message, baseError)] as Result<T, E>
}
}

type IHttpErrorFnsMap = {
Expand Down

0 comments on commit 04b14e1

Please sign in to comment.