Skip to content

Commit 6a2f2ee

Browse files
committed
feat: add the ability to override external errors
1 parent 711cae4 commit 6a2f2ee

File tree

4 files changed

+26
-31
lines changed

4 files changed

+26
-31
lines changed

src/components/File-Lesson.vue

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@ export default {
4646
onFiles: function (files) {
4747
this.uploadedFiles = files
4848
window.uploadedFiles = files
49-
// console.log({uploadedFiles: this.uploadedFiles})
5049
}
5150
}
5251
}

src/components/Lesson.vue

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -168,8 +168,8 @@ import MonacoEditor from 'vue-monaco-editor'
168168
import Explorer from './Explorer.vue'
169169
import Button from './Button.vue'
170170
import Header from './Header.vue'
171-
const CID = require('cids')
172-
const marked = require('marked')
171+
import CID from 'cids'
172+
import marked from 'marked'
173173
174174
const hljs = require('highlight.js/lib/highlight.js')
175175
hljs.registerLanguage('js', require('highlight.js/lib/languages/javascript'))
@@ -233,6 +233,7 @@ export default {
233233
concepts: self.$attrs.concepts,
234234
cachedCode: !!localStorage['cached' + self.$route.path],
235235
code: localStorage[self.cacheKey] || self.$attrs.code || self.defaultCode,
236+
overrideErrors: self.$attrs.overrideErrors,
236237
isFileLesson: self.isFileLesson,
237238
parsedText: marked(self.$attrs.text),
238239
parsedExercise: marked(self.$attrs.exercise || ''),
@@ -323,13 +324,14 @@ export default {
323324
let modules = {}
324325
if (this.$attrs.modules) modules = this.$attrs.modules
325326
if (this.isFileLesson) args.unshift(this.uploadedFiles)
327+
// Output external errors or not depending on flag
326328
let result = await _eval(code, ipfs, modules, args)
327-
328-
if (result && result.error) {
329+
if (!this.$attrs.overrideErrors && result && result.error) {
329330
Vue.set(output, 'test', result)
330331
this.lessonPassed = !!localStorage[this.lessonKey]
331332
return
332333
}
334+
// Run the `validate` function in the lesson
333335
let test = await this.$attrs.validate(result, ipfs, args)
334336
Vue.set(output, 'test', test)
335337
if (CID.isCID(result)) {
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
Let's add some files to IPFS using MFS. Since `files` is an array representing the files currently available to us in the browser, we'll need to loop through the array and use `files.write()` to add each file we find there to IPFS, one at a time. (Only have a single file uploaded? No problem!)
22

3-
Put your files in the root directory (`/`) and be sure to add the name of each file to the path when you add it. (Hint: The file object in the browser stores the filename as `file.name`.) Be sure to set up your options so that a new file is created when one isn't found at the given path.
3+
Put your files in the root directory (`/`) and be sure to add the name of each file to the path when you add it (**hint:** the file object in the browser stores the filename as `file.name`). Be sure to set up your options so that a new file is created when one isn't found at the given path.

src/tutorials/Mutable-File-System/03.vue

Lines changed: 19 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
:text="text"
44
:code="code"
55
:validate="validate"
6+
:overrideErrors="true"
67
:modules="modules"
78
:exercise="exercise"
89
lessonTitle="Add a new file to MFS">
@@ -15,47 +16,40 @@ import text from './03.md'
1516
import exercise from './03-exercise.md'
1617
1718
const validate = async (result, ipfs) => {
18-
// The code in this exercise does not have a return value since write doesn't
19-
// give anything back, so `result` should always be undefined and is irrelevant
20-
// for validation. Validation will be done by matching filenames between the
19+
// Validation will be done by matching filenames between the
2120
// uploadedFiles array and the files in IPFS and ensuring that the type of each
2221
// file in IPFS is 0 (file, not folder).
22+
// If IPFS errors out, we try to output a clearer version to the user. If that's
23+
// not possible, the error from IPFS will be the output.
2324
2425
let uploadedFiles = window.uploadedFiles || false
2526
2627
let ipfsFiles = await ipfs.files.ls('/', {long: true})
2728
console.log('Here\'s what\'s now in your root directory in IPFS:')
2829
console.log(ipfsFiles)
2930
30-
let uploadedFilenames = uploadedFiles.map( file => file.name.toString() ).sort()
31-
let ipfsFilenames = ipfsFiles.map( file => file.name.toString() ).sort()
31+
let uploadedFilenames = uploadedFiles.map(file => file.name.toString()).sort()
32+
let ipfsFilenames = ipfsFiles.map(file => file.name.toString()).sort()
3233
let itemsMatch = JSON.stringify(ipfsFilenames) === JSON.stringify(uploadedFilenames)
3334
let itemsAreFiles = ipfsFiles.every(file => file.type === 0)
3435
35-
if (uploadedFiles = false) {
36-
// shouldn't happen because you can't hit submit without uploading files
37-
return {'fail': 'Oops! You forgot to upload files to work with :('}
38-
} else if (ipfsFiles.length === 0) {
39-
// if somehow no files are written to IPFS
40-
return {'fail': 'Uh oh. Looks like no files made it into IPFS.'}
41-
} else if (!itemsAreFiles) {
42-
// if they forget the file name and just use a directory as the path
43-
// this never shows because there's a native error msg showing that's unclear
44-
return {'fail': 'Uh oh. It looks like you created a folder instead of a file. Did you forget to include a filename in your path?'}
45-
} else if (itemsMatch && itemsAreFiles) {
46-
return {'success': 'Success! Open your console to see what data is now in your root directory in IPFS.'}
47-
} else {
48-
return {'fail': 'Something we haven\'t anticipated is wrong. :('}
36+
if (itemsMatch && itemsAreFiles) {
37+
return { success: 'Success! Open your console to see what data is now in your root directory in IPFS.' }
38+
} else if (uploadedFiles = false) {
39+
// Shouldn't happen because you can't hit submit without uploading files
40+
return { fail: 'Oops! You forgot to upload files to work with :(' }
41+
} else if (result && result.error.message === 'No child name passed to addLink') {
42+
// Forgot the file name and just used a directory as the path
43+
return { fail: 'Uh oh. It looks like you created a folder instead of a file. Did you forget to include a filename in your path?' }
44+
} else if (result && result.error.message === 'file does not exist') {
45+
// Forgot the `{ create: true }` option
46+
return { fail: 'The file doesn\'t exist, so you need to create it. Maybe you forgot and option...' }
4947
}
5048
51-
// also wanted to make a custom error for if they forget {create: true}
52-
// but it also has a native error msg showing that's unclear
49+
// Output the default error if we haven't catched any
50+
return { error: result.error }
5351
}
5452
55-
//blah
56-
57-
58-
5953
const code = `const run = async (files) => {
6054
for (let file of files) {
6155
// your code to add one file to MFS goes here

0 commit comments

Comments
 (0)