UNPKG

@coursebuilder/core

Version:

Core package for Course Builder

1 lines 6.43 kB
{"version":3,"sources":["../../../src/lib/utils/logger.ts","../../../src/errors.ts"],"sourcesContent":["import { CourseBuilderError } from '../../errors.js'\n\nexport type WarningCode =\n\t| 'debug-enabled'\n\t| 'env-url-basepath-redundant'\n\t| 'env-url-basepath-mismatch'\n\nexport interface LoggerInstance extends Record<string, Function> {\n\twarn: (code: WarningCode) => void\n\terror: (error: Error) => void\n\tdebug: (message: string, metadata?: unknown) => void\n}\n\nconst red = '\\x1b[31m'\nconst yellow = '\\x1b[33m'\nconst grey = '\\x1b[38;5;246m'\nconst reset = '\\x1b[0m'\n\nexport const logger: LoggerInstance = {\n\terror(error) {\n\t\tconst name = error instanceof CourseBuilderError ? error.type : error.name\n\t\tconsole.error(\n\t\t\t`${red}[coursebuilder][error]${reset} ${name}: ${error.message}`,\n\t\t)\n\t\tif (\n\t\t\terror.cause &&\n\t\t\ttypeof error.cause === 'object' &&\n\t\t\t'err' in error.cause &&\n\t\t\terror.cause.err instanceof Error\n\t\t) {\n\t\t\tconst { err, ...data } = error.cause\n\t\t\tconsole.error(`${red}[coursebuilder][cause]${reset}:`, err.stack)\n\t\t\tif (data)\n\t\t\t\tconsole.error(\n\t\t\t\t\t`${red}[coursebuilder][details]${reset}:`,\n\t\t\t\t\tJSON.stringify(data, null, 2),\n\t\t\t\t)\n\t\t} else if (error.stack) {\n\t\t\tconsole.error(error.stack.replace(/.*/, '').substring(1))\n\t\t}\n\t},\n\twarn(code) {\n\t\tconst url = `https://warnings.coursebuilder.dev#${code}`\n\t\tconsole.warn(\n\t\t\t`${yellow}[coursebuilder][warn][${code}]${reset}`,\n\t\t\t`Read more: ${url}`,\n\t\t)\n\t},\n\tdebug(message, metadata) {\n\t\tconsole.log(\n\t\t\t`${grey}[coursebuilder][debug]:${reset} ${message}`,\n\t\t\tJSON.stringify(metadata, null, 2),\n\t\t)\n\t},\n}\n\n/**\n * Override the built-in logger with user's implementation.\n * Any `undefined` level will use the default logger.\n */\nexport function setLogger(\n\tnewLogger: Partial<LoggerInstance> = {},\n\tdebug?: boolean,\n) {\n\t// Turn off debug logging if `debug` isn't set to `true`\n\tif (!debug) logger.debug = () => {}\n\n\tif (newLogger.error) logger.error = newLogger.error\n\tif (newLogger.warn) logger.warn = newLogger.warn\n\tif (newLogger.debug) logger.debug = newLogger.debug\n}\n","type ErrorOptions = Error | Record<string, unknown>\n\ntype ErrorType =\n\t| 'AdapterError'\n\t| 'UnknownAction'\n\t| 'UnsupportedStrategy'\n\t| 'MissingAdapter'\n\t| 'MissingAdapterMethods'\n\nexport class CourseBuilderError extends Error {\n\t/** The error type. Used to identify the error in the logs. */\n\ttype: ErrorType\n\t/**\n\t * Determines on which page an error should be handled. Typically `signIn` errors can be handled in-page.\n\t * Default is `\"error\"`.\n\t * @internal\n\t */\n\tkind?: 'error'\n\tcause?: Record<string, unknown> & { err?: Error }\n\tconstructor(\n\t\tmessage?: string | Error | ErrorOptions,\n\t\terrorOptions?: ErrorOptions,\n\t) {\n\t\tif (message instanceof Error) {\n\t\t\tsuper(undefined, {\n\t\t\t\tcause: { err: message, ...(message.cause as any), ...errorOptions },\n\t\t\t})\n\t\t} else if (typeof message === 'string') {\n\t\t\tif (errorOptions instanceof Error) {\n\t\t\t\terrorOptions = { err: errorOptions, ...(errorOptions.cause as any) }\n\t\t\t}\n\t\t\tsuper(message, errorOptions)\n\t\t} else {\n\t\t\tsuper(undefined, message)\n\t\t}\n\t\tthis.name = this.constructor.name\n\t\t// @ts-expect-error https://github.com/microsoft/TypeScript/issues/3841\n\t\tthis.type = this.constructor.type ?? 'CourseBuilderError'\n\t\t// @ts-expect-error https://github.com/microsoft/TypeScript/issues/3841\n\t\tthis.kind = this.constructor.kind ?? 'error'\n\n\t\tError.captureStackTrace?.(this, this.constructor)\n\t\tconst url = `https://errors.authjs.dev#${this.type.toLowerCase()}`\n\t\tthis.message += `${this.message ? '. ' : ''}Read more at ${url}`\n\t}\n}\n\nexport class AdapterError extends CourseBuilderError {\n\tstatic type = 'AdapterError'\n}\n\nexport class UnsupportedStrategy extends CourseBuilderError {\n\tstatic type = 'UnsupportedStrategy'\n}\n\nexport class MissingAdapter extends CourseBuilderError {\n\tstatic type = 'MissingAdapter'\n}\n\nexport class MissingAdapterMethods extends CourseBuilderError {\n\tstatic type = 'MissingAdapterMethods'\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;;;;;;;;ACSO,IAAMA,sBAAN,MAAMA,4BAA2BC,MAAAA;EAUvC,YACCC,SACAC,cACC;;;AAXFC;;AAMAC;;;;;;AACAC;AAIE;;AACD,QAAIJ,mBAAmBD,OAAO;AAC7B,cAAMM,QAAW;QAChBD,OAAO;UAAEE,KAAKN;UAAS,GAAIA,QAAQI;UAAe,GAAGH;QAAa;MACnE,CAAA;IACD,WAAW,OAAOD,YAAY,UAAU;AACvC,UAAIC,wBAAwBF,OAAO;AAClCE,uBAAe;UAAEK,KAAKL;UAAc,GAAIA,aAAaG;QAAc;MACpE;AACA,cAAMJ,SAASC,YAAAA;IAChB,OAAO;AACN,cAAMI,QAAWL,OAAAA;IAClB;AACA,SAAKO,OAAO,KAAK,YAAYA;AAE7B,SAAKL,OAAO,KAAK,YAAYA,QAAQ;AAErC,SAAKC,OAAO,KAAK,YAAYA,QAAQ;AAErCJ,UAAMS,oBAAoB,MAAM,KAAK,WAAW;AAChD,UAAMC,MAAM,6BAA6B,KAAKP,KAAKQ,YAAW,CAAA;AAC9D,SAAKV,WAAW,GAAG,KAAKA,UAAU,OAAO,EAAA,gBAAkBS,GAAAA;EAC5D;AACD;AApCwCV;AAAjC,IAAMD,qBAAN;;;ADIP,IAAMa,MAAM;AACZ,IAAMC,SAAS;AACf,IAAMC,OAAO;AACb,IAAMC,QAAQ;AAEP,IAAMC,SAAyB;EACrCC,MAAMA,OAAK;AACV,UAAMC,OAAOD,iBAAiBE,qBAAqBF,MAAMG,OAAOH,MAAMC;AACtEG,YAAQJ,MACP,GAAGL,GAAAA,yBAA4BG,KAAAA,IAASG,IAAAA,KAASD,MAAMK,OAAO,EAAE;AAEjE,QACCL,MAAMM,SACN,OAAON,MAAMM,UAAU,YACvB,SAASN,MAAMM,SACfN,MAAMM,MAAMC,eAAeC,OAC1B;AACD,YAAM,EAAED,KAAK,GAAGE,KAAAA,IAAST,MAAMM;AAC/BF,cAAQJ,MAAM,GAAGL,GAAAA,yBAA4BG,KAAAA,KAAUS,IAAIG,KAAK;AAChE,UAAID;AACHL,gBAAQJ,MACP,GAAGL,GAAAA,2BAA8BG,KAAAA,KACjCa,KAAKC,UAAUH,MAAM,MAAM,CAAA,CAAA;IAE9B,WAAWT,MAAMU,OAAO;AACvBN,cAAQJ,MAAMA,MAAMU,MAAMG,QAAQ,MAAM,EAAA,EAAIC,UAAU,CAAA,CAAA;IACvD;EACD;EACAC,KAAKC,MAAI;AACR,UAAMC,MAAM,sCAAsCD,IAAAA;AAClDZ,YAAQW,KACP,GAAGnB,MAAAA,yBAA+BoB,IAAAA,IAAQlB,KAAAA,IAC1C,cAAcmB,GAAAA,EAAK;EAErB;EACAC,MAAMb,SAASc,UAAQ;AACtBf,YAAQgB,IACP,GAAGvB,IAAAA,0BAA8BC,KAAAA,IAASO,OAAAA,IAC1CM,KAAKC,UAAUO,UAAU,MAAM,CAAA,CAAA;EAEjC;AACD;AAMO,SAASE,UACfC,YAAqC,CAAC,GACtCJ,OAAe;AAGf,MAAI,CAACA;AAAOnB,WAAOmB,QAAQ,MAAA;IAAO;AAElC,MAAII,UAAUtB;AAAOD,WAAOC,QAAQsB,UAAUtB;AAC9C,MAAIsB,UAAUP;AAAMhB,WAAOgB,OAAOO,UAAUP;AAC5C,MAAIO,UAAUJ;AAAOnB,WAAOmB,QAAQI,UAAUJ;AAC/C;AAVgBG;","names":["CourseBuilderError","Error","message","errorOptions","type","kind","cause","undefined","err","name","captureStackTrace","url","toLowerCase","red","yellow","grey","reset","logger","error","name","CourseBuilderError","type","console","message","cause","err","Error","data","stack","JSON","stringify","replace","substring","warn","code","url","debug","metadata","log","setLogger","newLogger"]}