Skip to content

Commit

Permalink
more informative profile load error
Browse files Browse the repository at this point in the history
  • Loading branch information
Roy Razon committed Feb 5, 2024
1 parent b685ef9 commit 7ce4a64
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 5 deletions.
26 changes: 22 additions & 4 deletions packages/core/src/profile/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,12 @@ const listPersistence = ({ localDir }: { localDir: string }) => {
}
}

export class ProfileLoadError extends Error {
constructor(message: string) {
super(message)
}
}

export const localProfilesConfig = (
localDir: string,
fsFromUrl: (url: string, baseDir: string) => Promise<VirtualFS>,
Expand All @@ -74,22 +80,31 @@ export const localProfilesConfig = (
async function get(alias: string | undefined, opts?: { throwOnNotFound: boolean }): Promise<GetResult | undefined> {
const throwOrUndefined = () => {
if (opts?.throwOnNotFound) {
throw new Error(`Profile ${alias} not found`)
throw new ProfileLoadError(`Profile ${alias} not found`)
}
return undefined
}

const { profiles, current } = await listP.read()
const aliasToGet = alias ?? current
if (!aliasToGet) {
return throwOrUndefined()
if (opts?.throwOnNotFound) {
throw new ProfileLoadError('Profile not specified and no current profile is set')
}
return undefined
}
const locationUrl = profiles[aliasToGet]?.location
if (!locationUrl) {
if (opts?.throwOnNotFound) {
throw new ProfileLoadError(`No profile with alias ${aliasToGet}`)
}
return throwOrUndefined()
}
const tarSnapshotStore = await storeFromUrl(locationUrl)
const profileInfo = await profileStore(tarSnapshotStore).ref.info()
const profileInfo = await profileStore(tarSnapshotStore).ref.info({ throwOnNotFound: false })
if (!profileInfo) {
throw new ProfileLoadError(`Could not load profile "${aliasToGet}" at ${locationUrl}. The profile may have been deleted`)
}
return {
alias: aliasToGet,
location: locationUrl,
Expand Down Expand Up @@ -200,7 +215,10 @@ export const localProfilesConfig = (
throw new Error(`Profile ${alias} already exists`)
}
const tarSnapshotStore = await storeFromUrl(fromLocation)
const info = await profileStore(tarSnapshotStore).ref.info()
const info = await profileStore(tarSnapshotStore).ref.info({ throwOnNotFound: false })
if (!info) {
throw new Error(`Cannot import profile from ${fromLocation}. The profile may have been deleted`)
}
const newProfile = {
id: info.id,
alias,
Expand Down
9 changes: 8 additions & 1 deletion packages/core/src/profile/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,14 @@ const readLines = (buffer: Buffer | undefined) => {

const profileReader = (reader: FsReader) => {
const { readJsonOrThrow, readJSON } = jsonReader(reader)
const info = async () => await readJsonOrThrow<Profile>(filenames.info)
async function info(opts: { throwOnNotFound: false }): Promise<Profile | undefined>
async function info(opts: { throwOnNotFound: true }): Promise<Profile>
async function info(): Promise<Profile>
async function info(
{ throwOnNotFound = true }: { throwOnNotFound?: boolean } = { throwOnNotFound: true },
): Promise<Profile | undefined> {
return await (throwOnNotFound ? readJsonOrThrow<Profile> : readJSON<Profile>)(filenames.info)
}
return {
info,
driver: async () => (await info()).driver,
Expand Down

0 comments on commit 7ce4a64

Please sign in to comment.