# Troubleshooting and error codes

Every failing call returns a negative `sona_result`, and `sona_last_error(ctx)` gives a message and a `fix` line
written for that case. Print both; the fix usually says exactly what to change. The web API's errors carry the same
`fix` and a `docs` link ([web errors](web-api.md#errors)).

## Symptoms

### The avatar does not appear

- The avatar is still loading. `sona_avatar_add` returns at once and the avatar shows up once it is `READY`: an
  avatar that never shows up after `sona_avatar_add` is usually one whose context is not being updated. Check
  `sona_avatar_state_of`, and keep calling `sona_update` every frame.
- The avatar failed. Read the events (`sona_poll_event`): a `SONA_EVENT_FAILED` carries the code and message.
- The camera does not see it. Avatars stand about 1.8 m tall at the origin and face -Z at turn 0; start from a
  preset framing (`sona_camera_preset`) before moving the camera yourself.

### Avatars are black or too dark

- In an engine (Unity, Godot, Unreal) the avatar draws with the package's own material. A material of your own
  loses the baked lighting: use the shipped one.
- With the SDK's lighting on, a key light pointing up or a zero ambient leaves faces dark; start from
  `sona_lighting_defaults` ([lighting](guides/lighting.md)).

### It worked on my machine and fails on another

- `SONA_E_PLATFORM`: the download was made for other platforms. Download again with this platform ticked.
- `SONA_E_LICENCE`: the `sona-licence.json` next to the program is missing, or is another app's.

### Frame spikes when adding avatars

Building an avatar takes work on the owner thread. `build_budget_ms` in `sona_config` caps that work per update, and
worker threads take the rest; lower the budget, or add avatars a few at a time ([crowds](guides/crowd.md)).

### "403 origin" on a web page

The page's origin is not on the app's list. Add it on your app's page, with `http://localhost` (any port) while you
develop ([getting the SDK](web-api.md#sdk-download)). A `file://` page has no origin and never works.

## Error codes

Every error is a negative `sona_result`; `sona_result_name(r)` gives its name for a log line. `SONA_OK` is 0 and
`SONA_PENDING` is 1 (not finished: call again after `sona_update`).

| Code | Value | Cause and fix |
|---|---|---|
| `SONA_E_UNSUPPORTED` | -1 | No usable backend, API version or feature: the device lacks what the backend needs (Vulkan 1.1, OpenGL 3.3), or the call is for a module this build does not have. **Fix:** Pick another backend, or update the graphics driver. |
| `SONA_E_ORIGIN` | -2 | Web only: the page's origin is not one of the app's origins. Native builds never return it. **Fix:** Register the origin on your app's page. |
| `SONA_E_LICENCE` | -3 | The app has not accepted the SDK Licence, or the licence file is missing or bad. **Fix:** Accept the licence on your app's page, download again and pass that download's `sona-licence.json` unchanged. |
| `SONA_E_APP` | -4 | No such app, or it was turned off. **Fix:** Check the app id in your config and on your app's page. |
| `SONA_E_GATE` | -5 | 360sona could not be reached; the gate source streams the item files over https. **Fix:** Check the network; add a folder source so the game works offline. |
| `SONA_E_MANIFEST` | -6 | The avatar could not be read or built: the 1,000 bytes are not a manifest, or its items cannot be put together. **Fix:** Check the bytes with `sona_manifest_parse`, and that base64 text was not cut or changed. |
| `SONA_E_CONTEXT` | -7 | The graphics device failed or was lost. **Fix:** Recreate the renderer ([device loss](guides/device-loss.md)); the avatars stay in the context. |
| `SONA_E_READBACK` | -8 | A picture could not be read back from the GPU. **Fix:** Retry the snapshot; if it keeps failing, the device is lost. |
| `SONA_E_LIMIT` | -9 | Too many avatars or requests, over the daily use, or over the memory budget. **Fix:** Remove avatars you no longer show, raise `max_avatars` or the budget in `sona_config`, or wait for the daily use to reset. |
| `SONA_E_DESTROYED` | -10 | The context, avatar or renderer is gone. **Fix:** Do not use a handle after `sona_close`, `sona_avatar_remove` or `sona_renderer_destroy`. |
| `SONA_E_PLATFORM` | -11 | This build is not licensed for this platform. **Fix:** Download again with this platform ticked. |
| `SONA_E_BUILD` | -12 | This build was revoked. **Fix:** Download a new one from your app's page. |
| `SONA_E_ARGUMENT` | -13 | A bad argument: NULL where a pointer is needed, a struct whose `size` is not set, or a value out of range; the message names it. **Fix:** Fill structs with their `_defaults` call before setting fields. |
| `SONA_E_MEMORY` | -14 | The allocator returned NULL. **Fix:** Free memory, or give the context a larger budget or allocator. |
| `SONA_E_NOT_FOUND` | -15 | No source has that asset. **Fix:** For a folder source, check that it holds the item; else add the gate source. |
| `SONA_E_IO` | -16 | A source failed to read; a retry may work. **Fix:** Check the folder's permissions or the network, then try again. |
| `SONA_E_CORRUPT` | -17 | Bytes did not decode: a damaged file in a folder source, or a file that is not what its name says. **Fix:** Replace the file. |
| `SONA_E_NOT_READY` | -18 | The avatar is not ready. **Fix:** Wait for `READY` (call `sona_update` each frame) before reading its pose or joints. |
| `SONA_E_UNIMPLEMENTED` | -19 | The call is not in this build. **Fix:** Download a build with the module, or check which modules your platform has ([installing](install.md#optional-modules)). |

In a log:

```c
sona_config_defaults(&cfg);   /* then the licence and the sources */
sona_result r = sona_open(&cfg, &ctx);
if (r != SONA_OK) {
  sona_error_info e = sona_last_error(NULL);
  fprintf(stderr, "%s (%d): %s\n  %s\n", sona_result_name(r), (int)r, e.message, e.fix ? e.fix : "");
}
```

```cpp
try {
  sona::Config cfg;                        // then the licence and the sources
  sona::Context opened(cfg);
} catch (const sona::Error &e) {
  std::fprintf(stderr, "%s (%d): %s\n  %s\n", sona_result_name(e.code), (int)e.code, e.message.c_str(), e.fix.c_str());
}
```

```csharp
try
{
    using var opened = new SonaContext(new SonaOptions());   // then the licence and the sources
}
catch (SonaException e)
{
    Console.Error.WriteLine($"{e.Code} ({(int)e.Code}): {e.Message}\n  {e.Fix}");
}
```

```rust
match Context::open(Config::new()) {                 // then the licence and the sources
    Ok(opened) => { /* ... */ }
    Err(e) => eprintln!("{:?}: {}\n  {}", e.code, e.message, e.fix),
}
```

```python
try:
    opened = sona.Context()                           # then the licence and the sources
except sona.SonaError as e:
    print("%s (%d): %s\n  %s" % (e.name, e.code, e.message, e.fix))
```
