Skip to content

Commit

Permalink
feat: improved error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
field123 committed Sep 29, 2023
1 parent e2fb31a commit 1122d70
Show file tree
Hide file tree
Showing 19 changed files with 154 additions and 56 deletions.
1 change: 1 addition & 0 deletions packages/composable-cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
"ink-big-text": "1",
"ink-gradient": "2",
"ink-link": "2",
"ink-table": "^3.0.0",
"inquirer": "8.2.4",
"node-fetch": "2.7.0",
"open": "8",
Expand Down
19 changes: 13 additions & 6 deletions packages/composable-cli/src/commands/config/config-command.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import yargs from "yargs"
import Conf from "conf"
import { CommandContext, CommandHandlerFunction } from "../../types/command"
import { handleErrors } from "../../util/error-handler"
import {
ConfigCommandArguments,
ConfigCommandData,
Expand All @@ -16,7 +15,7 @@ export function configClearCommand(store: Conf): void {
export function createConfigCommand(
ctx: CommandContext,
): yargs.CommandModule<{}, ConfigCommandArguments> {
const { store, logger } = ctx
const { store, logger, handleErrors } = ctx

return {
command: "config",
Expand All @@ -26,16 +25,24 @@ export function createConfigCommand(
.command({
command: "list",
describe: "List all stored configuration",
handler: (_args) => {
handler: handleErrors(async (_args) => {
logger.info(JSON.stringify(store.store, null, 2))
},
return {
success: true,
data: {},
}
}),
})
.command({
command: "clear",
describe: "Clear all stored configuration",
handler: (_args) => {
handler: handleErrors(async (_args) => {
configClearCommand(store)
},
return {
success: true,
data: {},
}
}),
})
.example("$0 config list", "list all stored configuration")
.example("$0 config clear", "clear all stored configuration")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import {
CommandHandlerFunction,
RootCommandArguments,
} from "../../types/command"
import { handleErrors } from "../../util/error-handler"
import { renderInk } from "../../lib/ink/render-ink"
import React from "react"
import {
Expand All @@ -17,19 +16,20 @@ import { Feedback } from "../ui/feedback/feedback"
import { trackCommandHandler } from "../../util/track-command-handler"
import { isTTY } from "../../util/is-tty"
export function createFeedbackCommand(
ctx: CommandContext
ctx: CommandContext,
): yargs.CommandModule<RootCommandArguments, FeedbackCommandArguments> {
const { handleErrors } = ctx
return {
command: "feedback",
describe: "Feedback to the Composable CLI",
handler: handleErrors(
trackCommandHandler(ctx, createFeedbackCommandHandler)
trackCommandHandler(ctx, createFeedbackCommandHandler),
),
}
}

export function createFeedbackCommandHandler(
_ctx: CommandContext
_ctx: CommandContext,
): CommandHandlerFunction<
FeedbackCommandData,
FeedbackCommandError,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import {
CommandHandlerFunction,
CommandResult,
} from "../../../types/command"
import { handleErrors } from "../../../util/error-handler"
import { getRegion, resolveHostFromRegion } from "../../../util/resolve-region"
import { createApplicationKeys } from "../../../util/create-client-secret"
import { renderInk } from "../../../lib/ink/render-ink"
Expand Down Expand Up @@ -113,7 +112,9 @@ export function createD2CCommand(

return addSchemaOptionsToCommand(result, options)
},
handler: handleErrors(trackCommandHandler(ctx, createD2CCommandHandler)),
handler: ctx.handleErrors(
trackCommandHandler(ctx, createD2CCommandHandler),
),
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import {
CommandHandlerFunction,
RootCommandArguments,
} from "../../types/command"
import { handleErrors } from "../../util/error-handler"
import {
GenerateCommandArguments,
GenerateCommandData,
Expand Down Expand Up @@ -67,7 +66,7 @@ export function createGenerateCommand(
.demandCommand(1)
.strict()
},
handler: handleErrors(
handler: ctx.handleErrors(
trackCommandHandler(ctx, createGenerateCommandHandler),
),
}
Expand Down Expand Up @@ -125,7 +124,7 @@ export function createActiveStoreMiddleware(
return
}

return handleErrors(createSetStoreCommandHandler(ctx))(args)
return ctx.handleErrors(createSetStoreCommandHandler(ctx))(args)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import {
CommandHandlerFunction,
RootCommandArguments,
} from "../../types/command"
import { handleErrors } from "../../util/error-handler"
import {
InsightsCommandArguments,
InsightsCommandData,
Expand All @@ -30,7 +29,7 @@ export function createInsightsCommand(
})
.help()
},
handler: handleErrors(
handler: ctx.handleErrors(
trackCommandHandler(ctx, createInsightsCommandHandler),
),
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import {
AlgoliaIntegrationCommandError,
} from "./algolia-integration.types"
import { CommandContext, CommandHandlerFunction } from "../../../types/command"
import { handleErrors } from "../../../util/error-handler"
import { trackCommandHandler } from "../../../util/track-command-handler"
import {
createActiveStoreMiddleware,
Expand Down Expand Up @@ -77,7 +76,7 @@ export function createAlgoliaIntegrationCommand(
.fail(false)
.help()
},
handler: handleErrors(
handler: ctx.handleErrors(
trackCommandHandler(ctx, createAlgoliaIntegrationCommandHandler),
),
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import {
CommandHandlerFunction,
RootCommandArguments,
} from "../../types/command"
import { handleErrors } from "../../util/error-handler"
import {
IntegrationCommandArguments,
IntegrationCommandData,
Expand All @@ -29,7 +28,7 @@ export function createIntegrationCommand(
.demandCommand(1)
.strict()
},
handler: handleErrors(
handler: ctx.handleErrors(
trackCommandHandler(ctx, createIntegrationCommandHandler),
),
}
Expand Down
11 changes: 6 additions & 5 deletions packages/composable-cli/src/commands/login/login-command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import {
CommandHandlerFunction,
RootCommandArguments,
} from "../../types/command"
import { handleErrors } from "../../util/error-handler"
import {
LoginCommandArguments,
LoginCommandData,
Expand Down Expand Up @@ -81,7 +80,9 @@ export function createLoginCommand(
"strip-aliased": true,
})
},
handler: handleErrors(trackCommandHandler(ctx, createLoginCommandHandler)),
handler: ctx.handleErrors(
trackCommandHandler(ctx, createLoginCommandHandler),
),
}
}

Expand All @@ -97,9 +98,9 @@ export function createAuthenticationMiddleware(
return
}

return handleErrors(trackCommandHandler(ctx, createLoginCommandHandler))(
args,
)
return ctx.handleErrors(
trackCommandHandler(ctx, createLoginCommandHandler),
)(args)
}
}

Expand Down
9 changes: 5 additions & 4 deletions packages/composable-cli/src/commands/logout/logout-command.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import yargs from "yargs"
import { CommandContext, CommandHandlerFunction } from "../../types/command"
import { handleErrors } from "../../util/error-handler"
import {
LogoutCommandArguments,
LogoutCommandData,
Expand All @@ -14,17 +13,19 @@ import { handleClearCredentials } from "../../util/conf-store/store-credentials"
import { trackCommandHandler } from "../../util/track-command-handler"

export function createLogoutCommand(
ctx: CommandContext
ctx: CommandContext,
): yargs.CommandModule<{}, LogoutCommandArguments> {
return {
command: "logout",
describe: "Logout of the Composable CLI",
handler: handleErrors(trackCommandHandler(ctx, createLogoutCommandHandler)),
handler: ctx.handleErrors(
trackCommandHandler(ctx, createLogoutCommandHandler),
),
}
}

export function createLogoutCommandHandler(
ctx: CommandContext
ctx: CommandContext,
): CommandHandlerFunction<
LogoutCommandData,
LogoutCommandError,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import {
EPPaymentsCommandErrorAlreadyExists,
} from "./ep-payments-integration.types"
import { CommandContext, CommandHandlerFunction } from "../../../types/command"
import { handleErrors } from "../../../util/error-handler"
import { trackCommandHandler } from "../../../util/track-command-handler"
import {
createActiveStoreMiddleware,
Expand Down Expand Up @@ -55,7 +54,7 @@ export function createEPPaymentsCommand(
.fail(false)
.help()
},
handler: handleErrors(
handler: ctx.handleErrors(
trackCommandHandler(ctx, createEPPaymentsCommandHandler),
),
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import {
CommandHandlerFunction,
RootCommandArguments,
} from "../../types/command"
import { handleErrors } from "../../util/error-handler"

import { trackCommandHandler } from "../../util/track-command-handler"
import { createEPPaymentsCommand } from "./ep-payments/ep-payments-command"
Expand Down Expand Up @@ -32,7 +31,7 @@ export function createPaymentsCommand(
.demandCommand(1)
.strict()
},
handler: handleErrors(
handler: ctx.handleErrors(
trackCommandHandler(ctx, createPaymentsCommandHandler),
),
}
Expand Down
7 changes: 4 additions & 3 deletions packages/composable-cli/src/commands/store/store-command.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import {
CommandHandlerFunction,
RootCommandArguments,
} from "../../types/command"
import { handleErrors } from "../../util/error-handler"
import {
SetStoreCommandArguments,
SetStoreCommandData,
Expand Down Expand Up @@ -47,7 +46,9 @@ export function createStoreCommand(
.demandCommand(1)
.strict()
},
handler: handleErrors(trackCommandHandler(ctx, createStoreCommandHandler)),
handler: ctx.handleErrors(
trackCommandHandler(ctx, createStoreCommandHandler),
),
}
}

Expand All @@ -65,7 +66,7 @@ export function createSetStoreCommand(
})
.help()
},
handler: handleErrors(
handler: ctx.handleErrors(
trackCommandHandler(ctx, createSetStoreCommandHandler),
),
}
Expand Down
29 changes: 29 additions & 0 deletions packages/composable-cli/src/commands/ui/error/error.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import React from "react"
import { Box, Newline, Text } from "ink"
import Table from "ink-table"

export function ErrorTable({ data }: { data: Record<string, any> }) {
const errors = Object.keys(data).map((key) => {
return { key, value: data[key] }
})

return (
<Box flexDirection="column">
<Box marginY={1}>
<Text bold color="red">
There was an issue!
</Text>
</Box>
<Text>
To get support on this issue, report it on our slack community.
<Newline />
Join us at
<Text color="green">
{" "}
https://elasticpathcommunity.slack.com/join/shared_invite/zt-1upzq3nlc-O3sy1bT0UJYcOWEQQCtnqw
</Text>
</Text>
<Table data={errors} />
</Box>
)
}
10 changes: 10 additions & 0 deletions packages/composable-cli/src/commands/ui/login/welcome-note.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,16 @@ export function WelcomeNote({ name }: { name: string }) {
</Text>
<Newline />
<Text>A CLI for managing your Elasticpath powered storefront</Text>
<Newline />
<Box marginY={1}>
<Text>
To get support or ask any question, join us in our slack community.
<Newline />
<Text color="green">
https://elasticpathcommunity.slack.com/join/shared_invite/zt-1upzq3nlc-O3sy1bT0UJYcOWEQQCtnqw
</Text>
</Text>
</Box>
</Box>
</>
)
Expand Down
2 changes: 2 additions & 0 deletions packages/composable-cli/src/types/command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { logging } from "@angular-devkit/core"
import ansiColors from "ansi-colors"
import { Moltin } from "@moltin/sdk"
import { ComposableRc } from "../lib/composable-rc-schema"
import { ErrorHandler } from "../util/error-handler"

export type CommandResult<TData, TError> =
| {
Expand All @@ -34,6 +35,7 @@ export type CommandContext = {
epClient?: Moltin
composableRc?: ComposableRc
workspaceRoot?: string
handleErrors: ErrorHandler
}

export type RootCommandArguments = {
Expand Down
Loading

0 comments on commit 1122d70

Please sign in to comment.