Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor #120

Merged
merged 5 commits into from
Jan 21, 2021
Merged

Refactor #120

merged 5 commits into from
Jan 21, 2021

Conversation

padraicbc
Copy link
Contributor

  • Again some refactoring and handling unchecked errors. Wrapping errors ( wrapped with %w which may or may not suit. ) where appropriate and returning. Keeping any handling(log.Fatal...) outside to be able to easily test.

  • Addded common package with one simple func there to check errors. Can add more logic like the wrapping of errors etc.. to keep in one location.

  • Few other general changes like the fsnotify.Watcher logic which quits on inabilty to create/setup. Not the prettiest but works...

Copy link
Member

@jimafisk jimafisk left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is great @padraicbc, thanks for digging through all this. It helps me recognize some bad patterns I've gotten used to and gets us closer on feedback I've received about better error handling!

@@ -18,6 +18,7 @@ func Bundle() []byte {
//Externals: []string{"module", "fs", "path"},
Bundle: true,
})
// error?
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah that probably would be a good idea, bundle.go isn't actually being used at the moment... I had left it in there thinking it could be useful for pulling in libs or bundling for production, but we could probably remove it entirely for now.


// Get svelte compiler code from node_modules.
compiler, err := ioutil.ReadFile(tempBuildDir + "node_modules/svelte/compiler.js")
compiler, err := ioutil.ReadFile(
fmt.Sprintf("%snode_modules/svelte/compiler.js", tempBuildDir),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I noticed these in your last PR, curious what advantage fmt.Sprintf() has over concatenating with +? I figured it was performance, but doesn't seem the case: https://dev.to/pmalhaire/concatenate-strings-in-golang-a-quick-benchmark-4ahh

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+ would be faster bar you were maybe using Fprintf' with a responseWritervs callingw.Write(a+b+c)`. It is just personal preference/habit and probably did it on autopilot. I use it more so when you have a few substrings as I find it easier to see if fat finger syndome has occurred.

cmd/build/node_data_source.go Show resolved Hide resolved
cmd/root.go Show resolved Hide resolved
typeContentPath := fmt.Sprintf("content/%s", strings.Trim(typeName, " /"))
// !os.IsNotExist is true, the path exists. os.IsExist(err) == nil for Stat if file exists
if _, err := os.Stat(typeContentPath); !os.IsNotExist(err) {
fmt.Printf("A Type content source with the same name located at \"%s/\" already exists\n", typeContentPath)
return
// an error?
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, we could just return the Printf above as an error.

@@ -51,11 +52,12 @@ Optionally add a _blueprint.json file to define the default field structure for
Run: func(cmd *cobra.Command, args []string) {
typeName := args[0]

// shoud we stop here on error from either?
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, that's a good question... should you be able to complete either the data source or template if the other exists. There currently is the concept of a layout/content/ template without a content source (like 404.svelte). There are also types in the content/ folder without templates (No endpoint content: #13 (comment)). Yeah it might be best it if at least warns you about this. Currently it looks like this in the CLI:
plenti new type test

Creating new Type content source: content/test/
Creating new Type layout: layout/content/test.svelte

plenti new type test

A Type content source with the same name located at "content/test/" already exists
A Type layout with the same name located at "layout/content/test.svelte" already exists

rm -R content/test
plenti new type test

Creating new Type content source: content/test/
A Type layout with the same name located at "layout/content/test.svelte" already exists

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So there can be three scenarios:

  • A content/data source with a corresponding template.
  • A content/data source without corresponding template.
  • And a template without a data source.

Is there any situation where adding a template or data source breaks anything, say foo.svelte exists without content/foo(or vice versa) and then you plenti new type foo?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yup, so it should work in all cases:

  1. content with template = regular node endpoint(s) you can visit
  2. content without template = content you can pull into other nodes, but has no direct node endpoint itself
  3. template without content = rare, but currently used to handle 404 errors. You could eject the router to handle more cases.

Completing the set by either adding a template or data source shouldn't break anything, but might surprise the user with unintended consequences. For instance let's say the user inherited a site that put the main menu links into a content type called links. This existed as a content source without a template so there were no endpoints for the actual links (they may have pointed to nodes created by a different content type). If the user then tries to create a new content type called links for a different purpose, not realizing it already existed, the effect is a template will be created and it will expose all the existing links in the content source as node endpoints. In reality this would probably be discovered pretty quickly and with git the rollback would be trivial so I'm not super worried about this usecase - just wanted to put it out there.

typeContentPath := fmt.Sprintf("content/%s", strings.Trim(typeName, " /"))
// !os.IsNotExist is true, the path exists. os.IsExist(err) == nil for Stat if file exists
if _, err := os.Stat(typeContentPath); !os.IsNotExist(err) {
fmt.Printf("A Type content source with the same name located at \"%s/\" already exists\n", typeContentPath)
return
// an error?
return nil

}

if _, err := os.Stat(typeContentPath + ".json"); !os.IsNotExist(err) {
// error or not?
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could error this too per the notes above. Best UX I think would be adding promptui to confirm, but that can happen down the road. Either way these commands are little helpers that you could easily get around manually if you were unhappy with us cutting out early.

@jimafisk jimafisk merged commit ae4d25c into plentico:master Jan 21, 2021
@padraicbc
Copy link
Contributor Author

This is great @padraicbc, thanks for digging through all this. It helps me recognize some bad patterns I've gotten used to and gets us closer on feedback I've received about better error handling!

No worries. I think 95% of errors should be handled. Again I did a quick sweep thorugh the code so there are probably parts I have exited that you may want to give options or continue but it should be all in the build steps outside of the functions themselves so easy to test them and change the flow if needed.

@jimafisk
Copy link
Member

there are probably parts I have exited that you may want to give options or continue

I did end up killing the check in fsnotify.Create (d597450) because it stopped the local server on Linux, I think because of the way it performs temp file writing. Also saw your note (thank you) about ReferenceError: require is not defined when checking errors on SSRctx.RunScript so paused that for now just to get things working. I'll need to figure out what's going on there at some point. Overall super pumped to have better error handling, I appreciate all the time and care you took with this!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants