{"record":{"id":"8489dcc538c78dbf","repo":"crowdsecurity/crowdsec","slug":"machine-s-w-w","errorCode":null,"errorMessage":"machine '%s': %w: %w","messagePattern":"machine '(.+?)': %w: %w","errorType":"exception","errorClass":"QueryFail","httpStatus":null,"severity":"error","filePath":"pkg/database/machines.go","lineNumber":79,"sourceCode":"\t\treturn fmt.Errorf(\"unable to update base machine metrics in database: %w\", err)\n\t}\n\n\treturn nil\n}\n\nfunc (c *Client) CreateMachine(ctx context.Context, machineID *string, password *strfmt.Password, ipAddress string, isValidated bool, force bool, authType string) (*ent.Machine, error) {\n\thashPassword, err := bcrypt.GenerateFromPassword([]byte(*password), bcrypt.DefaultCost)\n\tif err != nil {\n\t\tc.Log.Warningf(\"CreateMachine: %s\", err)\n\t\treturn nil, HashError\n\t}\n\n\tmachineExist, err := c.Ent.Machine.\n\t\tQuery().\n\t\tWhere(machine.MachineIdEQ(*machineID)).\n\t\tSelect(machine.FieldMachineId).Strings(ctx)\n\tif err != nil {\n\t\treturn nil, fmt.Errorf(\"machine '%s': %w: %w\", *machineID, err, QueryFail)\n\t}\n\n\tif len(machineExist) > 0 {\n\t\tif force {\n\t\t\t_, err := c.Ent.Machine.Update().Where(machine.MachineIdEQ(*machineID)).SetPassword(string(hashPassword)).Save(ctx)\n\t\t\tif err != nil {\n\t\t\t\tc.Log.Warningf(\"CreateMachine : %s\", err)\n\t\t\t\treturn nil, fmt.Errorf(\"machine '%s': %w\", *machineID, UpdateFail)\n\t\t\t}\n\n\t\t\tmachine, err := c.QueryMachineByID(ctx, *machineID)\n\t\t\tif err != nil {\n\t\t\t\treturn nil, fmt.Errorf(\"machine '%s': %w: %w\", *machineID, err, QueryFail)\n\t\t\t}\n\n\t\t\treturn machine, nil\n\t\t}\n","sourceCodeStart":61,"sourceCodeEnd":97,"githubUrl":"https://github.com/crowdsecurity/crowdsec/blob/909b5157986a2b2c2163300fdaef5ed01289f7d2/pkg/database/machines.go#L61-L97","documentation":"A machine-related DB operation failed and the machine ID is double-wrapped with a sentinel error class (e.g. HashError from bcrypt in CreateMachine, or a similar machine sentinel). The %w:%w shape lets callers errors.Is-match the sentinel while still seeing the driver error; the machine name identifies the registration involved.","triggerScenarios":"The `Machine.Query().Where(machine.MachineIdEQ(...)).Strings(ctx)` lookup errors — DB down, ctx canceled, or the machines table missing/corrupt. Reached via `cscli machines add`, TLS auth, or LAPI registration.","commonSituations":"Database not initialized (`cscli machines add` on a fresh install before `crowdsec` first run created the schema); MySQL/Postgres outage; corrupted SQLite file.","solutions":["Ensure DB schema exists — start crowdsec once or run the migrate step before `cscli machines add`","Check DB connectivity and credentials in /etc/crowdsec/config.yaml","If SQLite, run `PRAGMA integrity_check` on the db file","Retry the machine creation once the DB is healthy"],"exampleFix":"// caller-side retry\nfor i := 0; i < 3; i++ {\n    m, err := client.CreateMachine(ctx, &id, pass, ip, true, false, authType)\n    if err == nil || !errors.Is(err, database.QueryFail) { return m, err }\n    time.Sleep(time.Second)\n}","handlingStrategy":"try-catch","validationCode":"// before creating, confirm DB is initialized\nif _, err := os.Stat(dbPath); os.IsNotExist(err) { runSchemaInit() }","typeGuard":null,"tryCatchPattern":"m, err := client.CreateMachine(ctx, &id, pass, ip, true, false, auth)\nif err != nil && errors.Is(err, database.QueryFail) {\n    return fmt.Errorf(\"db not ready for machine registration: %w\", err)\n}","preventionTips":["Run crowdsec once (or the migrate step) before `cscli machines add` on fresh installs","Test DB connectivity from config before CLI operations","Watch for partial upgrades leaving schema outdated"],"tags":["database","machines","ent"],"backgroundTag":"database-query-failed","analyzedSha":"909b5157986a2b2c2163300fdaef5ed01289f7d2","analyzedAt":"2026-09-06T12:27:26.012Z","contentChangedAt":"2026-09-06T12:27:26.012Z","schemaVersion":2},"datasetVersion":"2026-09-14T00:17:10.932Z"}