Skip to content

Commit

Permalink
Make error handling consistent
Browse files Browse the repository at this point in the history
  • Loading branch information
gwenwindflower committed Apr 1, 2024
1 parent cd1c868 commit 0eaa83a
Show file tree
Hide file tree
Showing 12 changed files with 35 additions and 33 deletions.
10 changes: 5 additions & 5 deletions forms.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,29 +131,29 @@ If you use an existing directory, tbd will overwrite any existing files with the
confirm_form.WithTheme(huh.ThemeCatppuccin())
err := intro_form.Run()
if err != nil {
log.Fatal(err)
log.Fatalf("Error running intro form %v\n", err)
}
if formResponse.UseDbtProfile {
err = dbt_form.Run()
} else {
err = manual_form.Run()
}
if err != nil {
log.Fatal(err)
log.Fatalf("Error running connectiond details form %v\n", err)
}
if formResponse.GenerateDescriptions {
err = llm_form.Run()
if err != nil {
log.Fatal(err)
log.Fatalf("Error running LLM features form %v\n", err)
}
}
err = dir_form.Run()
if err != nil {
log.Fatal(err)
log.Fatalf("Error running build directory form %v\n", err)
}
err = confirm_form.Run()
if err != nil {
log.Fatal(err)
log.Fatalf("Error running confirmation form %v\n", err)
}
return formResponse
}
14 changes: 7 additions & 7 deletions generate_column_desc.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,11 +114,11 @@ func GenerateColumnDescriptions(tables shared.SourceTables) {
tests_prompt := fmt.Sprintf(tests_prompt, table_name, column_name)
desc_resp, err := GetGroqResponse(desc_prompt)
if err != nil {
log.Fatalf("Failed to get response from Groq for description: %v", err)
log.Fatalf("Failed to get response from Groq for description: %v\n", err)
}
tests_resp, err := GetGroqResponse(tests_prompt)
if err != nil {
log.Fatalf("Failed to get response from Groq for tests: %v", err)
log.Fatalf("Failed to get response from Groq for tests: %v\n", err)
}
if len(desc_resp.Choices) > 0 {
tables.SourceTables[i].Columns[j].Description = desc_resp.Choices[0].Message.Content
Expand Down Expand Up @@ -151,30 +151,30 @@ func GetGroqResponse(prompt string) (GroqResponse, error) {
}
payload, err := json.Marshal(meta)
if err != nil {
log.Fatalf("Failed to marshal JSON: %v", err)
log.Fatalf("Failed to marshal JSON: %v\n", err)
}
req, err := http.NewRequest(http.MethodPost, URL, bytes.NewBuffer(payload))
if err != nil {
log.Fatalf("Unable to create request: %v", err)
log.Fatalf("Unable to create request: %v\n", err)
}
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Authorization", "Bearer "+os.Getenv("GROQ_API_KEY"))
client := http.Client{}
response, err := client.Do(req)
if err != nil {
log.Fatalf("Request failed: %v", err)
log.Fatalf("Request failed: %v\n", err)
}
defer response.Body.Close()

body, err := io.ReadAll(response.Body)
if err != nil {
log.Fatalf("Cannot read response body: %v", err)
log.Fatalf("Cannot read response body: %v\n", err)
}

var resp GroqResponse
err = json.Unmarshal(body, &resp)
if err != nil {
log.Fatalf("Failed to unmarshal JSON: %v", err)
log.Fatalf("Failed to unmarshal JSON: %v\n", err)
}
return resp, nil
}
Expand Down
4 changes: 2 additions & 2 deletions get_dbt_profile.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func GetDbtProfile(dbtProfile string) (*DbtProfile, error) {
yamlFile, err := os.ReadFile(path)
if err == nil {
if err := yaml.Unmarshal(yamlFile, &profileMap); err != nil {
log.Fatalf("could not unmarshal dbt profile: %v", err)
log.Fatalf("Could not unmarshal dbt profile: %v\n", err)
}

if profile, ok := profileMap[dbtProfile]; ok {
Expand All @@ -31,6 +31,6 @@ func GetDbtProfile(dbtProfile string) (*DbtProfile, error) {
if selectedProfile != nil {
return selectedProfile, nil
} else {
return nil, fmt.Errorf("could not find profile: %s", dbtProfile)
return nil, fmt.Errorf("no profile named %s", dbtProfile)
}
}
4 changes: 1 addition & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,6 @@ func main() {

formResponse := Forms()
if !formResponse.Confirm {
// TODO: Read up on error types in Go,
// do a pass on the full codebase to make them correct + consistent
log.Fatal("⛔ User cancelled.")
}
connectionDetails := SetConnectionDetails(formResponse)
Expand All @@ -49,7 +47,7 @@ func main() {

tables, err := sourcerer.GetSources(ctx, connectionDetails)
if err != nil {
log.Fatalf("Error getting sources: %v", err)
log.Fatalf("Error getting sources: %v\n", err)
}

dbElapsed = time.Since(connectionStart).Seconds()
Expand Down
10 changes: 7 additions & 3 deletions prep_build_dir.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
package main

import (
"log"
"os"
)

func PrepBuildDir(buildDir string) {
_, err := os.Stat(buildDir)
if err != nil {
log.Fatalf("Failed to get directory info %v\n", err)
}
if os.IsNotExist(err) {
errDir := os.MkdirAll(buildDir, 0755)
if errDir != nil {
panic(err)
dirErr := os.MkdirAll(buildDir, 0755)
if dirErr != nil {
log.Fatalf("Failed to create directory %v\n", dirErr)
}
}
}
2 changes: 1 addition & 1 deletion set_connection_details.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ func SetConnectionDetails(formResponse FormResponse) shared.ConnectionDetails {
if formResponse.UseDbtProfile {
profile, err := GetDbtProfile(formResponse.DbtProfile)
if err != nil {
log.Fatal(err)
log.Fatalf("Could not get dbt profile %v\n", err)
}
connectionDetails = shared.ConnectionDetails{
ConnType: profile.Outputs[formResponse.DbtProfileOutput].ConnType,
Expand Down
2 changes: 1 addition & 1 deletion sourcerer/connect_to_db.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func (sfc *SnowflakeConnection) ConnectToDB(ctx context.Context, connectionDetai
defer cancel()
db, err = sql.Open("snowflake", connStr)
if err != nil {
log.Fatal(err)
log.Fatalf("Could not connect to Snowflake %v\n", err)
}
return db, cancel, err
}
2 changes: 1 addition & 1 deletion sourcerer/get_columns.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func GetColumns(db *sql.DB, ctx context.Context, table shared.SourceTable, conne
for rows.Next() {
column := shared.Column{}
if err := rows.Scan(&column.Name, &column.DataType); err != nil {
return nil, err
log.Fatalf("Error scanning columns for table %s: %v\n", table.Name, err)
}
columns = append(columns, column)
}
Expand Down
7 changes: 3 additions & 4 deletions sourcerer/get_sources.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,21 +24,20 @@ func GetSources(ctx context.Context, connectionDetails shared.ConnectionDetails)
db, cancel, err := dbConn.ConnectToDB(ctx, connectionDetails)
defer cancel()
if err != nil {
log.Fatalf("couldn't connect to database: %v", err)
log.Fatalf("Couldn't connect to database: %v\n", err)
}
rows, err := db.QueryContext(ctx, fmt.Sprintf("SELECT table_name FROM information_schema.tables where table_schema = '%s'", connectionDetails.Schema))
if err != nil {
return tables, err
log.Fatalf("Error fetching tables: %v\n", err)
}
defer rows.Close()
for rows.Next() {
var table shared.SourceTable
if err := rows.Scan(&table.Name); err != nil {
return tables, err
log.Fatalf("Error scanning tables: %v\n", err)
}
tables.SourceTables = append(tables.SourceTables, table)
}

PutColumnsOnTables(ctx, db, tables, connectionDetails)
}
default:
Expand Down
2 changes: 1 addition & 1 deletion sourcerer/put_columns_on_tables.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func PutColumnsOnTables(ctx context.Context, db *sql.DB, tables shared.SourceTab

columns, err := GetColumns(db, ctx, tables.SourceTables[i], connectionDetails)
if err != nil {
log.Printf("Error fetching columns for table %s: %v\n", tables.SourceTables[i].Name, err)
log.Fatalf("Error fetching columns for table %s: %v\n", tables.SourceTables[i].Name, err)
return
}

Expand Down
7 changes: 4 additions & 3 deletions write_staging_models.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package main
import (
"embed"
"fmt"
"log"
"os"
"strings"
"sync"
Expand All @@ -24,19 +25,19 @@ func WriteStagingModels(tables shared.SourceTables, buildDir string) {
tmpl := template.New("staging_template.sql").Funcs(template.FuncMap{"lower": strings.ToLower})
tmpl, err := tmpl.ParseFS(stagingTemplate, "staging_template.sql")
if err != nil {
panic(err)
log.Fatalf("Failed to parse template %v\n", err)
}

filename := fmt.Sprintf(buildDir + "/stg_" + strings.ToLower(table.Name) + ".sql")
outputFile, err := os.Create(filename)
if err != nil {
panic(err)
log.Fatalf("Failed to create file %v\n", err)
}
defer outputFile.Close()

err = tmpl.Execute(outputFile, table)
if err != nil {
panic(err)
log.Fatalf("Failed to execute template %v\n", err)
}
}(table)
}
Expand Down
4 changes: 2 additions & 2 deletions write_yaml.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ import (
func WriteYAML(tables shared.SourceTables, buildDir string) {
yamlData, err := yaml.Marshal(tables)
if err != nil {
log.Fatal(err)
log.Fatalf("Failed to marshal data into YAML %v\n", err)
}

writeError := os.WriteFile(buildDir+"/_sources.yml", yamlData, 0644)
if writeError != nil {
log.Fatalf("Failed to write file %v", writeError)
log.Fatalf("Failed to write file %v\n", writeError)
}
}

0 comments on commit 0eaa83a

Please sign in to comment.