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

fix: treat 404 as "no object with that ID" #80

Merged
merged 1 commit into from
Mar 8, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 33 additions & 37 deletions apps/cacvote-mark/backend/src/cacvote-server/client.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
import { Buffer } from 'buffer';
import { Result, asyncResultBlock, err, ok } from '@votingworks/basics';
import {
Optional,
Result,
asyncResultBlock,
err,
ok,
} from '@votingworks/basics';
import fetch, { Headers, Request } from 'cross-fetch';
import { safeParse } from '@votingworks/types';
import { ZodError, z } from 'zod';
Expand Down Expand Up @@ -31,55 +37,40 @@ export class Client {
* Check that the server is responding.
*/
async checkStatus(): Promise<ClientResult<void>> {
const statusResult = await this.get('/api/status');

if (statusResult.isErr()) {
return statusResult;
}

const response = statusResult.ok();
if (!response.ok) {
return err({ type: 'network', message: response.statusText });
}
return asyncResultBlock(async (bail) => {
const response = (await this.get('/api/status')).okOrElse(bail);

return ok();
if (!response.ok) {
bail({ type: 'network', message: response.statusText });
}
});
}

/**
* Create an object on the server.
*/
async createObject(signedObject: SignedObject): Promise<ClientResult<Uuid>> {
const postResult = await this.post(
'/api/objects',
JSON.stringify(signedObject)
);

if (postResult.isErr()) {
return postResult;
}

const response = postResult.ok();
if (!response.ok) {
return err({ type: 'network', message: response.statusText });
}

const uuidResult = safeParse(UuidSchema, await response.text());
return asyncResultBlock(async (bail) => {
const response = (
await this.post('/api/objects', JSON.stringify(signedObject))
).okOrElse(bail);

if (uuidResult.isErr()) {
return err({
type: 'schema',
error: uuidResult.err(),
message: uuidResult.err().message,
});
}
if (!response.ok) {
bail({ type: 'network', message: response.statusText });
}

return uuidResult;
return safeParse(UuidSchema, await response.text()).okOrElse<ZodError>(
(error) => bail({ type: 'schema', error, message: error.message })
);
});
}

/**
* Retrieve an object from the server.
*/
async getObjectById(uuid: Uuid): Promise<ClientResult<SignedObject>> {
async getObjectById(
uuid: Uuid
): Promise<ClientResult<Optional<SignedObject>>> {
const SignedObjectRawSchema = z.object({
payload: z.string(),
certificates: z.string(),
Expand All @@ -88,6 +79,11 @@ export class Client {
return asyncResultBlock(async (bail) => {
const response = (await this.get(`/api/objects/${uuid}`)).okOrElse(bail);

if (response.status === 404) {
// not found
return undefined;
}

if (!response.ok) {
bail({ type: 'network', message: response.statusText });
}
Expand Down Expand Up @@ -133,7 +129,7 @@ export class Client {
).okOrElse(bail);

if (!response.ok) {
return err({ type: 'network', message: response.statusText });
bail({ type: 'network', message: response.statusText });
}

const entries = safeParse(
Expand Down