Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit345770f

Browse files
committed
load error markdown in middleware
Signed-off-by: shmck <shawn.j.mckay@gmail.com>
1 parent9fdb5df commit345770f

File tree

10 files changed

+87
-50
lines changed

10 files changed

+87
-50
lines changed
File renamed without changes.
File renamed without changes.

‎src/actions/tutorialConfig.ts

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,28 +13,40 @@ interface TutorialConfigParams {
1313
consttutorialConfig=async({ config, alreadyConfigured}:TutorialConfigParams):Promise<E.ErrorMessage|void>=>{
1414
if(!alreadyConfigured){
1515
// setup git, add remote
16-
awaitgit.initIfNotExists().catch((error:Error)=>{
17-
return{
16+
constinitError:E.ErrorMessage|void=awaitgit.initIfNotExists().catch(
17+
(error:Error):E.ErrorMessage=>({
1818
type:'GitNotFound',
1919
message:error.message,
20-
}
21-
})
20+
}),
21+
)
22+
23+
if(initError){
24+
returninitError
25+
}
2226

2327
// verify that internet is connected, remote exists and branch exists
24-
awaitgit.checkRemoteConnects(config.repo).catch((error:Error)=>{
25-
return{
28+
constremoteConnectError:E.ErrorMessage|void=awaitgit.checkRemoteConnects(config.repo).catch(
29+
(error:Error):E.ErrorMessage=>({
2630
type:'FailedToConnectToGitRepo',
2731
message:error.message,
28-
}
29-
})
32+
}),
33+
)
34+
35+
if(remoteConnectError){
36+
returnremoteConnectError
37+
}
3038

3139
// TODO if remote not already set
32-
awaitgit.setupCodeRoadRemote(config.repo.uri).catch((error:Error)=>{
33-
return{
40+
constcoderoadRemoteError:E.ErrorMessage|void=awaitgit.setupCodeRoadRemote(config.repo.uri).catch(
41+
(error:Error):E.ErrorMessage=>({
3442
type:'GitRemoteAlreadyExists',
3543
message:error.message,
36-
}
37-
})
44+
}),
45+
)
46+
47+
if(coderoadRemoteError){
48+
returncoderoadRemoteError
49+
}
3850
}
3951

4052
awaitvscode.commands.executeCommand(COMMANDS.CONFIG_TEST_RUNNER,config.testRunner)

‎src/channel/index.ts

Lines changed: 32 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,11 @@ import logger from '../services/logger'
1111
importContextfrom'./context'
1212
import{versionasgitVersion}from'../services/git'
1313
import{openWorkspace,checkWorkspaceEmpty}from'../services/workspace'
14+
import{readFile}from'fs'
15+
import{join}from'path'
16+
import{promisify}from'util'
17+
18+
constreadFileAsync=promisify(readFile)
1419

1520
interfaceChannel{
1621
receive(action:T.Action):Promise<void>
@@ -40,7 +45,9 @@ class Channel implements Channel {
4045
publicreceive=async(action:T.Action)=>{
4146
// action may be an object.type or plain string
4247
constactionType:string=typeofaction==='string' ?action :action.type
43-
constonError=(error:T.ErrorMessage)=>this.send({type:'ERROR',payload:{ error}})
48+
// const onError = (error: T.ErrorMessage) => this.send({ type: 'ERROR', payload: { error }})
49+
50+
// console.log(`ACTION: ${actionType}`)
4451

4552
switch(actionType){
4653
case'EDITOR_ENV_GET':
@@ -107,13 +114,10 @@ class Channel implements Channel {
107114
thrownewError('Invalid tutorial to continue')
108115
}
109116
constcontinueConfig:TT.TutorialConfig=tutorialContinue.config
110-
awaittutorialConfig(
111-
{
112-
config:continueConfig,
113-
alreadyConfigured:true,
114-
},
115-
onError,
116-
)
117+
awaittutorialConfig({
118+
config:continueConfig,
119+
alreadyConfigured:true,
120+
})
117121
// update the current stepId on startup
118122
vscode.commands.executeCommand(COMMANDS.SET_CURRENT_STEP,action.payload)
119123
return
@@ -156,6 +160,23 @@ class Channel implements Channel {
156160
}
157161
// send to webview
158162
publicsend=async(action:T.Action)=>{
163+
// Error middleware
164+
if(action?.payload?.error?.type){
165+
// load error markdown message
166+
consterror=action.payload.error
167+
consterrorMarkdownFile=join(__dirname,'..','..','errors',`${action.payload.error.type}.md`)
168+
consterrorMarkdown=awaitreadFileAsync(errorMarkdownFile).catch(()=>{
169+
// onError(new Error(`Error Markdown file not found for ${action.type}`))
170+
})
171+
172+
console.log(`ERROR:\n${errorMarkdown}`)
173+
174+
if(errorMarkdown){
175+
// add a clearer error message for the user
176+
error.message=`${errorMarkdown}\n${error.message}`
177+
}
178+
}
179+
159180
// action may be an object.type or plain string
160181
constactionType:string=typeofaction==='string' ?action :action.type
161182
switch(actionType){
@@ -170,8 +191,9 @@ class Channel implements Channel {
170191
saveCommit()
171192
}
172193

173-
constsuccess=awaitthis.postMessage(action)
174-
if(!success){
194+
// send message
195+
constsentToClient=awaitthis.postMessage(action)
196+
if(!sentToClient){
175197
thrownewError(`Message post failure:${JSON.stringify(action)}`)
176198
}
177199
}

‎src/services/git/index.ts

Lines changed: 13 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
import*asTTfrom'typings/tutorial'
22
importnodefrom'../node'
33
importloggerfrom'../logger'
4-
importonErrorfrom'../sentry/onError'
54

65
constgitOrigin='coderoad'
76

8-
conststashAllFiles=async()=>{
7+
conststashAllFiles=async():Promise<never|void>=>{
98
// stash files including untracked (eg. newly created file)
109
const{ stdout, stderr}=awaitnode.exec(`git stash --include-untracked`)
1110
if(stderr){
@@ -14,7 +13,7 @@ const stashAllFiles = async () => {
1413
}
1514
}
1615

17-
constcherryPickCommit=async(commit:string,count=0):Promise<void>=>{
16+
constcherryPickCommit=async(commit:string,count=0):Promise<never|void>=>{
1817
if(count>1){
1918
console.warn('cherry-pick failed')
2019
return
@@ -38,7 +37,7 @@ const cherryPickCommit = async (commit: string, count = 0): Promise<void> => {
3837
SINGLE git cherry-pick %COMMIT%
3938
if fails, will stash all and retry
4039
*/
41-
exportfunctionloadCommit(commit:string):Promise<void>{
40+
exportfunctionloadCommit(commit:string):Promise<never|void>{
4241
returncherryPickCommit(commit)
4342
}
4443

@@ -47,7 +46,7 @@ export function loadCommit(commit: string): Promise<void> {
4746
git commit -am '${level}/${step} complete'
4847
*/
4948

50-
exportasyncfunctionsaveCommit(message:string):Promise<void>{
49+
exportasyncfunctionsaveCommit(message:string):Promise<never|void>{
5150
const{ stdout, stderr}=awaitnode.exec(`git commit -am '${message}'`)
5251
if(stderr){
5352
console.error(stderr)
@@ -56,7 +55,7 @@ export async function saveCommit(message: string): Promise<void> {
5655
logger(['save with commit & continue stdout',stdout])
5756
}
5857

59-
exportasyncfunctionclear():Promise<void>{
58+
exportasyncfunctionclear():Promise<Error|void>{
6059
try{
6160
// commit progress to git
6261
const{ stderr}=awaitnode.exec('git reset HEAD --hard && git clean -fd')
@@ -83,23 +82,21 @@ export async function version(): Promise<string | null> {
8382
returnnull
8483
}
8584

86-
asyncfunctioninit():Promise<void>{
85+
asyncfunctioninit():Promise<Error|void>{
8786
const{ stderr}=awaitnode.exec('git init')
8887
if(stderr){
89-
consterror=newError('Error initializing Git')
90-
onError(error)
91-
throwerror
88+
thrownewError('Error initializing Git')
9289
}
9390
}
9491

95-
exportasyncfunctioninitIfNotExists():Promise<void>{
92+
exportasyncfunctioninitIfNotExists():Promise<never|void>{
9693
consthasGitInit=node.exists('.git')
9794
if(!hasGitInit){
9895
awaitinit()
9996
}
10097
}
10198

102-
exportasyncfunctioncheckRemoteConnects(repo:TT.TutorialRepo):Promise<boolean|Error>{
99+
exportasyncfunctioncheckRemoteConnects(repo:TT.TutorialRepo):Promise<never|void>{
103100
// check for git repo
104101
constexternalRepoExists=awaitnode.exec(`git ls-remote --exit-code --heads${repo.uri}`)
105102
if(externalRepoExists.stderr){
@@ -114,10 +111,9 @@ export async function checkRemoteConnects(repo: TT.TutorialRepo): Promise<boolea
114111
if(!stdout||!stdout.length){
115112
thrownewError('Tutorial branch does not exist')
116113
}
117-
returntrue
118114
}
119115

120-
exportasyncfunctionaddRemote(repo:string):Promise<void>{
116+
exportasyncfunctionaddRemote(repo:string):Promise<never|void>{
121117
const{ stderr}=awaitnode.exec(`git remote add${gitOrigin}${repo} && git fetch${gitOrigin}`)
122118
if(stderr){
123119
constalreadyExists=stderr.match(`${gitOrigin} already exists.`)
@@ -145,14 +141,13 @@ export async function checkRemoteExists(): Promise<boolean> {
145141
}
146142
}
147143

148-
exportasyncfunctionsetupCodeRoadRemote(repo:string):Promise<void>{
144+
exportasyncfunctionsetupCodeRoadRemote(repo:string):Promise<never|void>{
149145
// check coderoad remote not taken
150146
consthasRemote=awaitcheckRemoteExists()
151147
// git remote add coderoad tutorial
152148
// git fetch coderoad
153-
if(!hasRemote){
154-
awaitaddRemote(repo)
155-
}else{
149+
if(hasRemote){
156150
thrownewError('A CodeRoad remote is already configured')
157151
}
152+
awaitaddRemote(repo)
158153
}

‎src/services/workspace/index.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
import*asvscodefrom'vscode'
22
import*asfsfrom'fs'
3+
import{promisify}from'util'
4+
5+
constreadDir=promisify(fs.readdir)
36

47
exportconstopenWorkspace=()=>{
58
constopenInNewWindow=false
@@ -9,7 +12,7 @@ export const openWorkspace = () => {
912
exportconstcheckWorkspaceEmpty=async(dirname:string)=>{
1013
letfiles
1114
try{
12-
files=awaitfs.promises.readdir(dirname)
15+
files=awaitreadDir(dirname)
1316
}catch(error){
1417
thrownewError('Failed to check workspace')
1518
}

‎typings/error.d.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
exporttypeErrorMessageView='FULL_PAGE'|'NOTIFY'|'NONE'
22

3-
exporttypeErrorMessageType='UnknownError'|'GitNotFound'|'FailedToConnectToGitRepo'|'GitProjectAlreadyExists'
3+
exporttypeErrorMessageType=
4+
|'UnknownError'
5+
|'GitNotFound'
6+
|'FailedToConnectToGitRepo'
7+
|'GitProjectAlreadyExists'
8+
|'GitRemoteAlreadyExists'
49

510
exporttypeErrorMessage={
611
type:ErrorMessageType
Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import*asReactfrom'react'
2+
import*asEfrom'typings/error'
23
import{css,jsx}from'@emotion/core'
3-
importonErrorfrom'../../services/sentry/onError'
4+
importMarkdownfrom'../Markdown'
45

56
conststyles={
67
container:{
@@ -13,17 +14,16 @@ const styles = {
1314
}
1415

1516
interfaceProps{
16-
error?:Error
17+
error?:E.ErrorMessage
1718
}
1819

19-
constErrorView=({ error}:Props)=>{
20-
// log error
20+
constErrorMarkdown=({ error}:Props)=>{
2121
React.useEffect(()=>{
2222
if(error){
23+
// log error
2324
console.log(error)
24-
onError(error)
2525
}
26-
},[])
26+
},[error])
2727

2828
if(!error){
2929
returnnull
@@ -32,9 +32,9 @@ const ErrorView = ({ error }: Props) => {
3232
return(
3333
<divcss={styles.container}>
3434
<h1>Error</h1>
35-
<div>{JSON.stringify(error)}</div>
35+
<Markdown>{error.message}</Markdown>
3636
</div>
3737
)
3838
}
3939

40-
exportdefaultErrorView
40+
exportdefaultErrorMarkdown

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp