Skip to content
Merged
Show file tree
Hide file tree
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
9 changes: 9 additions & 0 deletions app/classes/Error.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
class HttpException {
code: number
message: string
constructor(code: number, message: string) {
this.code = code
this.message = message
}
}
export { HttpException }
5 changes: 3 additions & 2 deletions app/classes/Matrix.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { range } from "../utils/misc"

import { EchelonType, RowOperation } from "../types/Matrix"
import { leadingEntryIndex } from "../utils/Matrix"
import { HttpException } from "./Error"

import * as _ from "lodash"

Expand Down Expand Up @@ -250,7 +251,7 @@ class Matrix extends BaseMatrix {
class SquareMatrix extends BaseMatrix {
constructor(props: IMatrix) {
if (props.rows != props.columns)
throw new Error("Row and column counts do not match")
throw new HttpException(400, "Row and column counts do not match")
super(props)
}

Expand Down Expand Up @@ -305,7 +306,7 @@ class SquareMatrix extends BaseMatrix {
inverse() {
// if matrix is singular, throw error that the matrix is singular
if (this.calcDeterminant() === 0)
throw new Error("Matrix is singular; No inverse exists")
throw new HttpException(400, "Matrix is singular; No inverse exists")

const rrefActions = this.toRREF()

Expand Down
10 changes: 7 additions & 3 deletions app/controllers/matrix.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { VercelRequest, VercelResponse } from "@vercel/node"
import * as _ from "lodash"

import { Matrix, SquareMatrix } from "../classes/Matrix"

Expand All @@ -23,7 +24,8 @@ const calcDeterminant = (req: VercelRequest, res: VercelResponse) => {
const determinant = squareMatrix.calcDeterminant()
res.json(determinant)
} catch (err) {
res.status(500).json({ message: err.message })
const errorCode = _.get(err, "code", 500)
res.status(errorCode).json({ message: err.message })
}
}

Expand All @@ -50,7 +52,8 @@ const reduceRREF = (req: VercelRequest, res: VercelResponse) => {
matrix: matrix.entries,
})
} catch (err) {
res.status(500).json({ message: err.message })
const errorCode = _.get(err, "code", 500)
res.status(errorCode).json({ message: err.message })
}
}

Expand All @@ -77,7 +80,8 @@ const calcInverse = (req: VercelRequest, res: VercelResponse) => {
matrix: squareMatrix.entries,
})
} catch (err) {
res.status(500).json({ message: err.message })
const errorCode = _.get(err, "code", 500)
res.status(errorCode).json({ message: err.message })
}
}

Expand Down
12 changes: 6 additions & 6 deletions app/tests/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,15 +93,15 @@ describe("API tests", () => {
chai.expect(res.body).to.equal(0)
})
})
it("should obtain 500 for 3 by 2 matrix", async () => {
it("should obtain 400 for 3 by 2 matrix", async () => {
await chai
.request(url)
.get(route)
.query({
matrix: "[[1,2],[3,4],[5,6]]",
})
.then((res) => {
chai.expect(res.status).to.equal(500)
chai.expect(res.status).to.equal(400)
chai
.expect(res.body.message)
.to.equal("Row and column counts do not match")
Expand All @@ -124,29 +124,29 @@ describe("API tests", () => {
chai.expect(res.status).to.equal(200)
})
})
it("should obtain 500 for 3 by 2 matrix", async () => {
it("should obtain 400 for 3 by 2 matrix", async () => {
await chai
.request(url)
.get(route)
.query({
matrix: "[[1,2],[3,4],[5,6]]",
})
.then((res) => {
chai.expect(res.status).to.equal(500)
chai.expect(res.status).to.equal(400)
chai
.expect(res.body.message)
.to.equal("Row and column counts do not match")
})
})
it("should obtain 500 for singular square matrix", async () => {
it("should obtain 400 for singular square matrix", async () => {
await chai
.request(url)
.get(route)
.query({
matrix: "[[1,2],[0,0]]",
})
.then((res) => {
chai.expect(res.status).to.equal(500)
chai.expect(res.status).to.equal(400)
chai
.expect(res.body.message)
.to.equal("Matrix is singular; No inverse exists")
Expand Down
2 changes: 1 addition & 1 deletion pages/matrix.js
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ const Page = () => {
setLoading(false)
} catch (err) {
setLoading(false)
setError(err.message)
setError(err.response.data.message)
}
}

Expand Down