| 184 | ) |
| 185 | |
| 186 | func registerDefinition(def *Definition) *Definition { |
| 187 | definitionsMu.Lock() |
| 188 | defer definitionsMu.Unlock() |
| 189 | fullName := def.FullName() |
| 190 | if existing := definitions[fullName]; existing != nil { |
| 191 | if existing.code != def.code { |
| 192 | panic(fmt.Errorf( |
| 193 | "error %s with code %d already defined with code %d", |
| 194 | fullName, def.code, existing.code, |
| 195 | )) |
| 196 | } |
| 197 | if existing.messageFormat != def.messageFormat { |
| 198 | panic(fmt.Errorf( |
| 199 | "error %s with message format %q already defined with message format %q", |
| 200 | fullName, def.messageFormat, existing.messageFormat, |
| 201 | )) |
| 202 | } |
| 203 | if !reflect.DeepEqual(existing.publicAttributes, def.publicAttributes) { |
| 204 | panic(fmt.Errorf( |
| 205 | "error %s with public attributes %q already defined with public attributes %q", |
| 206 | fullName, def.publicAttributes, existing.publicAttributes, |
| 207 | )) |
| 208 | } |
| 209 | return existing |
| 210 | } |
| 211 | definitions[fullName] = def |
| 212 | return def |
| 213 | } |
| 214 | |
| 215 | // Define defines a registered error of type Unknown. |
| 216 | func Define(name, messageFormat string, publicAttributes ...string) *Definition { |