-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
support for thermal ingredients && better error handling
- Loading branch information
1 parent
447a0dd
commit 374f434
Showing
18 changed files
with
241 additions
and
135 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,22 @@ | ||
import { Logger } from './logger' | ||
|
||
export class IllegalShapeError extends Error { | ||
constructor(message: string, readonly input?: any) { | ||
super(input ? `${message}: ${JSON.stringify(input)}` : message) | ||
} | ||
} | ||
|
||
export function tryCatching<T>(logger: Logger | undefined, run: () => T): T | null { | ||
try { | ||
return run() | ||
} catch (error) { | ||
if (error instanceof IllegalShapeError) { | ||
logger?.warn((error as Error).message) | ||
return null | ||
} | ||
|
||
// TODO catch zod errors? | ||
|
||
throw error | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import { FluidTag, IngredientInput, ItemTagSchema } from '../../../common/ingredient' | ||
import zod from 'zod' | ||
import { ItemSchema } from '../../../common/result' | ||
import { omit } from 'lodash-es' | ||
import { IllegalShapeError } from '../../../error' | ||
|
||
const ThermalFluidTagSchema = zod.object({ | ||
fluid_tag: zod.string(), | ||
amount: zod.number(), | ||
}) | ||
|
||
type ThermalFluidTag = zod.infer<typeof ThermalFluidTagSchema> | ||
|
||
type ThermalItemList = Readonly<{ | ||
value: ThermalIngredientInput[] | ||
count?: number | ||
}> | ||
|
||
export type ThermalIngredientInput = Exclude<IngredientInput, FluidTag> | ThermalFluidTag | ThermalItemList | ||
|
||
function fromThermalList(input: ThermalItemList): IngredientInput { | ||
return input.value.map(it => { | ||
if (it && typeof it === 'object') { | ||
if ('item' in it) return { ...ItemSchema.parse(it), count: input.count } | ||
if ('tag' in it) return { ...ItemTagSchema.parse(it), count: input.count } | ||
} | ||
throw new IllegalShapeError('thermal array ingredients may only be of type item/itemtag', it) | ||
}) | ||
} | ||
|
||
export function fromThermalIngredient(input: ThermalIngredientInput): IngredientInput { | ||
if (input && typeof input === 'object') { | ||
if ('value' in input) return fromThermalList(input) | ||
|
||
if ('fluid_tag' in input) { | ||
return { ...omit(input, 'fluid_tag'), fluidTag: input.fluid_tag } | ||
} | ||
} | ||
|
||
return input | ||
} | ||
|
||
export function toThermalIngredient(input: IngredientInput): ThermalIngredientInput { | ||
if (input && typeof input === 'object') { | ||
if ('fluidTag' in input) { | ||
return <ThermalFluidTag>{ ...omit(input, 'fluidTag'), fluid_tag: input.fluidTag } | ||
} | ||
} | ||
|
||
return input | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.