packageDocumentation module import deepAssign from "deep-assign" ; import { GetMetaDataFunction , Guid , HttpStatus , IModelHubStatus , LogFunction , Logger } from "@itwin/core-bentley" ; import { ResponseError } from "../itwin-client/Request" ; import { WsgError , WSStatus } from "../wsg/WsgClient" ; import { IModelHubClientLoggerCategory } from "../IModelHubClientLoggerCategories" ; const loggerCategory = IModelHubClientLoggerCategory . IModelHub ; internal export class IModelHubError extends WsgError { /** Extended data of the error. */ public data : any ; private static _idPrefix : string = "iModelHub." ; internal public constructor ( errorNumber , message ? , getMetaData ? ) { super ( errorNumber , message , getMetaData ) ; } param error returns private static getErrorId ( error ) { const id = IModelHubStatus [ error . slice ( IModelHubError . _idPrefix . length ) as keyof typeof IModelHubStatus ] ; return id ? id : IModelHubStatus . Unknown ; } param id returns private static requiresExtendedData ( id ) { switch ( id ) { case IModelHubStatus . LockOwnedByAnotherBriefcase : case IModelHubStatus . iModelAlreadyExists : case IModelHubStatus . FileAlreadyExists : case IModelHubStatus . PullIsRequired : case IModelHubStatus . CodeStateInvalid : case IModelHubStatus . CodeReservedByAnotherBriefcase : case IModelHubStatus . ConflictsAggregate : return true ; default : return false ; } } /**
     * Make extended data available publicly.
     */ private copyExtendedData ( ) { this . data = this . _data ; } param id param message returns internal public static fromId ( id , message ) { const error = new IModelHubError ( id ) ; error . name = IModelHubStatus [ id ] ; error . message = message ; return error ; } param response returns internal public static override parse ( response , log = true ) { const wsgError = super . parse ( response , false ) ; if ( wsgError instanceof WsgError && wsgError . name && wsgError . name . startsWith ( IModelHubError . _idPrefix ) ) { const errorId = IModelHubError . getErrorId ( wsgError . name ) ; const error = new IModelHubError ( errorId ) ; deepAssign ( error , wsgError ) ; error . errorNumber = errorId ; if ( IModelHubError . requiresExtendedData ( error . errorNumber ) ) { error . copyExtendedData ( ) ; } if ( log ) error . log ( ) ; return error ; } if ( log ) wsgError . log ( ) ; return wsgError ; } param error param response internal public static override shouldRetry ( error , response ) { if ( response === undefined || response === null ) { return super . shouldRetry ( error , response ) ; } if ( super . parseHttpStatus ( response . statusType ) === HttpStatus . Success ) { return false ; } const parsedError = IModelHubError . parse ( { response } , false ) ; if ( ! ( parsedError instanceof WsgError ) ) { return super . shouldRetry ( error , response ) ; } if ( ( parsedError instanceof IModelHubError ) ) { return false ; } const errorCodesToRetry = [ HttpStatus . ServerError , WSStatus . Unknown , ] ; const errorStatus = super . getErrorStatus ( parsedError . name !== undefined ? super . getWSStatusId ( parsedError . name ) : WSStatus . Unknown , response . statusType ) ; return errorCodesToRetry . includes ( errorStatus ) ; } internal public getLogLevel ( ) { switch ( this . errorNumber ) { case IModelHubStatus . AnotherUserPushing : case IModelHubStatus . PullIsRequired : case IModelHubStatus . LockOwnedByAnotherBriefcase : case IModelHubStatus . CodeReservedByAnotherBriefcase : return Logger . logWarning ; default : return Logger . logError ; } } internal public override log ( ) { ( this . getLogLevel ( ) ) ( loggerCategory , this . logMessage ( ) , ( ) => this . getMetaData ( ) ) ; } } internal export class IModelHubClientError extends IModelHubError { param id param message returns internal public static override fromId ( id , message ) { const error = new IModelHubClientError ( id , message ) ; error . log ( ) ; return error ; } param argumentName returns internal public static undefinedArgument ( argumentName ) { return this . fromId ( IModelHubStatus . UndefinedArgumentError , `Argument ${ argumentName } is null or undefined` ) ; } param argumentName returns internal public static invalidArgument ( argumentName ) { return this . fromId ( IModelHubStatus . InvalidArgumentError , `Argument ${ argumentName } has an invalid value.` ) ; } param argumentName returns internal public static missingDownloadUrl ( argumentName ) { return this . fromId ( IModelHubStatus . MissingDownloadUrlError , `Supplied ${ argumentName } must include download URL. Use selectDownloadUrl() when getting ${ argumentName }.` ) ; } returns internal public static initializationTimeout ( ) { return this . fromId ( IModelHubStatus . InitializationTimeout , `Timed out waiting for Seed File initialization.` ) ; } returns internal public static browser ( ) { return this . fromId ( IModelHubStatus . NotSupportedInBrowser , "Operation is not supported in browser." ) ; } returns internal public static fileHandler ( ) { return this . fromId ( IModelHubStatus . FileHandlerNotSet , "File handler is required to be set for file download / upload." ) ; } returns internal public static fileNotFound ( ) { return this . fromId ( IModelHubStatus . FileNotFound , "Could not find the file to upload." ) ; } } internal export class ArgumentCheck { public static defined ( argumentName , argument ? , allowEmpty = false ) { if ( argument === undefined || argument === null || ( argument === "" && ! allowEmpty ) ) throw IModelHubClientError . undefinedArgument ( argumentName ) ; } public static definedNumber ( argumentName , argument ? ) { if ( typeof argument !== "number" ) throw IModelHubClientError . undefinedArgument ( argumentName ) ; } public static valid ( argumentName , argument ? ) { if ( ! argument ) throw IModelHubClientError . invalidArgument ( argumentName ) ; } public static validGuid ( argumentName , argument ? ) { this . defined ( argumentName , argument ) ; if ( ! Guid . isGuid ( argument ! ) ) throw IModelHubClientError . invalidArgument ( argumentName ) ; } public static nonEmptyArray ( argumentName , argument ? ) { this . defined ( argumentName , argument ) ; if ( argument ! . length < 1 ) throw IModelHubClientError . invalidArgument ( argumentName ) ; } /** Check if Briefcase Id is valid. */ private static isBriefcaseIdValid ( briefcaseId ) { return briefcaseId > 1 && briefcaseId < 16 * 1024 * 1024 ; } /** Check if Briefcase Id argument is valid. */ public static validBriefcaseId ( argumentName , argument ? ) { this . definedNumber ( argumentName , argument ) ; if ( ! this . isBriefcaseIdValid ( argument ! ) ) throw IModelHubClientError . invalidArgument ( argumentName ) ; } private static isValidChangeSetId ( changeSetId , allowEmpty = false ) { if ( changeSetId . length === 0 ) return allowEmpty ; const pattern = new RegExp ( "^[0-9A-Fa-f]+$" ) ; return changeSetId . length === 40 && pattern . test ( changeSetId ) ; } public static validChangeSetId ( argumentName , argument ? , allowEmpty = false ) { this . defined ( argumentName , argument , allowEmpty ) ; if ( ! this . isValidChangeSetId ( argument ! , allowEmpty ) ) throw IModelHubClientError . invalidArgument ( argumentName ) ; } } public export class AggregateResponseError extends Error { /** Errors that happened over multiple requests. */ public errors : ResponseError [ ] = [ ] ; }