88} from '@hive/usage-common' ;
99import * as tb from '@sinclair/typebox' ;
1010import * as tc from '@sinclair/typebox/compiler' ;
11+ import * as tbe from '@sinclair/typebox/errors' ;
1112import { invalidRawOperations , rawOperationsSize , totalOperations , totalReports } from './metrics' ;
1213import { TokensResponse } from './tokens' ;
1314import { isValidOperationBody } from './usage-processor-1' ;
@@ -34,7 +35,7 @@ export const usageProcessorV2 = traceInlineSync(
3435 token : TokensResponse ,
3536 targetRetentionInDays : number | null ,
3637 ) :
37- | { success : false ; errors : Array < tc . ValueError > }
38+ | { success : false ; errors : Array < ValueError > }
3839 | {
3940 success : true ;
4041 report : RawReport ;
@@ -307,6 +308,12 @@ function isUnixTimestamp(x: number) {
307308 return unixTimestampRegex . test ( String ( x ) ) ;
308309}
309310
311+ tbe . SetErrorFunction ( param => {
312+ return param . schema [ tb . Kind ] === 'UnixTimestampInMs'
313+ ? 'Expected valid unix timestamp in milliseconds'
314+ : tbe . DefaultErrorFunction ( param ) ;
315+ } ) ;
316+
310317tb . TypeRegistry . Set < number > ( 'UnixTimestampInMs' , ( _ , value ) =>
311318 typeof value === 'number' ? isUnixTimestamp ( value ) : false ,
312319) ;
@@ -361,14 +368,20 @@ type ReportType = tb.Static<typeof ReportSchema>;
361368
362369const ReportModel = tc . TypeCompiler . Compile ( ReportSchema ) ;
363370
371+ interface ValueError {
372+ path : string ;
373+ message : string ;
374+ errors ?: ValueError [ ] ;
375+ }
376+
364377export function decodeReport (
365378 report : unknown ,
366- ) : { success : true ; report : ReportType } | { success : false ; errors : tc . ValueError [ ] } {
367- const errors = getFirstN ( ReportModel . Errors ( report ) , 5 ) ;
368- if ( errors . length ) {
379+ ) : { success : true ; report : ReportType } | { success : false ; errors : Array < ValueError > } {
380+ const errors = ReportModel . Errors ( report ) ;
381+ if ( ReportModel . Errors ( report ) . First ( ) ) {
369382 return {
370383 success : false ,
371- errors,
384+ errors : getTypeBoxErrors ( errors ) ,
372385 } ;
373386 }
374387
@@ -378,19 +391,15 @@ export function decodeReport(
378391 } ;
379392}
380393
381- function getFirstN < TValue > ( iterable : Iterable < TValue > , max : number ) : TValue [ ] {
382- let counter = 0 ;
383- const items : Array < TValue > = [ ] ;
384- for ( const item of iterable ) {
385- items . push ( item ) ;
386- counter ++ ;
387-
388- if ( counter >= max ) {
389- break ;
390- }
391- }
392-
393- return items ;
394+ function getTypeBoxErrors ( errors : tc . ValueErrorIterator ) : Array < ValueError > {
395+ return Array . from ( errors ) . map ( error => {
396+ const errors = error . errors . flatMap ( errors => getTypeBoxErrors ( errors ) ) ;
397+ return {
398+ path : error . path ,
399+ message : error . message ,
400+ errors : errors . length ? errors : undefined ,
401+ } ;
402+ } ) ;
394403}
395404
396405const DAY_IN_MS = 86_400_000 ;
0 commit comments