Exemple #1
0
// Stage 3: Delete Two Annotations
func stage3() {
	log.Println("stage 3")

	minimal, err := pdf.Open("h7-minimal.pdf")
	if err != nil {
		log.Fatalln(errgo.Details(err))
	}

	// log.Println("minimal:", minimal)

	// annotation array
	minimal.Add(pdf.IndirectObject{
		ObjectReference: pdf.ObjectReference{ObjectNumber: 7},
		Object: pdf.Array{
			pdf.ObjectReference{ObjectNumber: 10},
			pdf.ObjectReference{ObjectNumber: 11},
		},
	})

	minimal.Free(8)
	minimal.Free(9)

	err = minimal.Save()
	if err != nil {
		log.Fatalln(errgo.Details(err))
	}
}
Exemple #2
0
func handlerLogger(fn Handler, w http.ResponseWriter, r *http.Request, p httprouter.Params) {
	l := NewHandlerLogEntry(r)
	l.Info("started handling request")

	starttime := time.Now()
	err := fn(w, r, p)
	duration := time.Since(starttime)

	l.Data["duration"] = duration

	code := http.StatusOK

	if err != nil {
		code = err.Code
	}

	l.Data["status"] = code
	l.Data["text_status"] = http.StatusText(code)

	if err != nil {
		if err.Error != nil {
			l.Data["error_message"] = err.Error

			l.Debug(errgo.Details(err.Error))
			l.Error("completed handling request")

			scode := strconv.Itoa(err.Code)
			http.Error(w, scode+" ("+http.StatusText(err.Code)+") - "+err.Error.Error(), err.Code)
		}
	} else {
		l.Info("completed handling request")
	}
}
Exemple #3
0
// ErrExit will check if the underlying error is nil and if its not it will
// print a debug and fatal message and exit the program.
func ErrExit(err error) {
	cerr := err.(errgo.Wrapper)

	if cerr.Underlying() != nil {
		log.Debug(errgo.Details(err))
		log.Fatal(err)
	}
}
Exemple #4
0
// Stage 2: Modify Text of One Annotation
func stage2() {
	log.Println("stage 2")

	minimal, err := pdf.Open("h7-minimal.pdf")
	if err != nil {
		log.Fatalln(errgo.Details(err))
	}

	annotation := minimal.Get(pdf.ObjectReference{ObjectNumber: 10}).(pdf.Dictionary)
	annotation[pdf.Name("Contents")] = pdf.String("Modified Text #3")
	minimal.Add(pdf.IndirectObject{
		ObjectReference: pdf.ObjectReference{ObjectNumber: 10},
		Object:          annotation,
	})

	err = minimal.Save()
	if err != nil {
		log.Fatalln(errgo.Details(err))
	}
}
Exemple #5
0
func checkErr(c *gc.C, err, underlying error, msg string, details string, cause error) {
	c.Assert(err, gc.NotNil)
	c.Assert(err.Error(), gc.Equals, msg)
	if err, ok := err.(errgo.Wrapper); ok {
		c.Assert(err.Underlying(), gc.Equals, underlying)
	} else {
		c.Assert(underlying, gc.IsNil)
	}
	c.Assert(errgo.Cause(err), gc.Equals, cause)
	wantDetails := replaceLocations(details)
	c.Assert(errgo.Details(err), gc.Equals, wantDetails)
}
Exemple #6
0
func (*errorsSuite) TestDetails(c *gc.C) {
	c.Assert(errgo.Details(nil), gc.Equals, "[]")

	otherErr := fmt.Errorf("other")
	checkErr(c, otherErr, nil, "other", "[{other}]", otherErr)

	err0 := &embed{errgo.New("foo").(*errgo.Err)} //err TestStack#0
	checkErr(c, err0, nil, "foo", "[{$TestStack#0$: foo}]", err0)

	err1 := &embed{errgo.Notef(err0, "bar").(*errgo.Err)} //err TestStack#1
	checkErr(c, err1, err0, "bar: foo", "[{$TestStack#1$: bar} {$TestStack#0$: foo}]", err1)

	err2 := errgo.Mask(err1) //err TestStack#2
	checkErr(c, err2, err1, "bar: foo", "[{$TestStack#2$: } {$TestStack#1$: bar} {$TestStack#0$: foo}]", err2)
}
Exemple #7
0
func TestDetails(t *testing.T) {
	if details := errgo.Details(nil); details != "[]" {
		t.Fatalf("errgo.Details(nil) got %q want %q", details, "[]")
	}

	otherErr := fmt.Errorf("other")
	checkErr(t, otherErr, nil, "other", "[{other}]", otherErr)

	err0 := &embed{errgo.New("foo").(*errgo.Err)} //err TestStack#0
	checkErr(t, err0, nil, "foo", "[{$TestStack#0$: foo}]", err0)

	err1 := &embed{errgo.Notef(err0, "bar").(*errgo.Err)} //err TestStack#1
	checkErr(t, err1, err0, "bar: foo", "[{$TestStack#1$: bar} {$TestStack#0$: foo}]", err1)

	err2 := errgo.Mask(err1) //err TestStack#2
	checkErr(t, err2, err1, "bar: foo", "[{$TestStack#2$: } {$TestStack#1$: bar} {$TestStack#0$: foo}]", err2)
}
Exemple #8
0
func checkErr(t *testing.T, err, underlying error, msg string, details string, cause error) {
	if err == nil {
		t.Fatalf("err is nil; want %q", msg)
	}
	if err.Error() != msg {
		t.Fatalf("unexpected message: want %q; got %q", msg, err.Error())
	}
	if err, ok := err.(errgo.Wrapper); ok {
		if err.Underlying() != underlying {
			t.Fatalf("unexpected underlying error: want %q; got %v", underlying, err.Underlying())
		}
	} else if underlying != nil {
		t.Fatalf("no underlying error found; want %q", underlying)
	}
	if errgo.Cause(err) != cause {
		t.Fatalf("unexpected cause: want %#v; got %#v", cause, errgo.Cause(err))
	}
	wantDetails := replaceLocations(details)
	if gotDetails := errgo.Details(err); gotDetails != wantDetails {
		t.Fatalf("unexpected details: want %q; got %q", wantDetails, gotDetails)
	}
}
Exemple #9
0
func (fn Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
	if e := fn(w, r); e != nil {
		log.Printf("request error: %s", errgo.Details(e.Err))
		http.Error(w, e.Message, e.Code)
	}
}
Exemple #10
0
func Execute() {
	err := RootCmd.Execute()
	if err != nil {
		log.Debug(errgo.Details(err))
	}
}
Exemple #11
0
// Stage 4: Add Three Annotations
func stage4() {
	log.Println("stage 4")

	minimal, err := pdf.Open("h7-minimal.pdf")
	if err != nil {
		log.Fatalln(errgo.Details(err))
	}

	annotationsObj := minimal.Get(pdf.ObjectReference{ObjectNumber: 7})
	annotations := annotationsObj.(pdf.Array)

	// annotation 8 1
	ref, err := minimal.Add(pdf.IndirectObject{
		ObjectReference: pdf.ObjectReference{ObjectNumber: 8, GenerationNumber: 1},
		Object: pdf.Dictionary{
			pdf.Name("Type"):    pdf.Name("Annot"),
			pdf.Name("Subtype"): pdf.Name("Text"),
			pdf.Name("Rect"): pdf.Array{
				pdf.Integer(58),
				pdf.Integer(657),
				pdf.Integer(172),
				pdf.Integer(742),
			},
			pdf.Name("Contents"): pdf.String("New Text #1"),
			pdf.Name("Open"):     pdf.Boolean(true),
		},
	})
	if err != nil {
		log.Fatal(errgo.Details(err))
	}
	annotations = append(annotations, ref)

	// annotation 9 1
	ref, err = minimal.Add(pdf.IndirectObject{
		ObjectReference: pdf.ObjectReference{ObjectNumber: 9, GenerationNumber: 1},
		Object: pdf.Dictionary{
			pdf.Name("Type"):    pdf.Name("Annot"),
			pdf.Name("Subtype"): pdf.Name("Text"),
			pdf.Name("Rect"): pdf.Array{
				pdf.Integer(389),
				pdf.Integer(459),
				pdf.Integer(570),
				pdf.Integer(537),
			},
			pdf.Name("Contents"): pdf.String("New Text #2"),
			pdf.Name("Open"):     pdf.Boolean(false),
		},
	})
	if err != nil {
		log.Fatal(errgo.Details(err))
	}
	annotations = append(annotations, ref)

	// annotation 12 0
	ref, err = minimal.Add(pdf.IndirectObject{
		ObjectReference: pdf.ObjectReference{ObjectNumber: 12},
		Object: pdf.Dictionary{
			pdf.Name("Type"):    pdf.Name("Annot"),
			pdf.Name("Subtype"): pdf.Name("Text"),
			pdf.Name("Rect"): pdf.Array{
				pdf.Integer(44),
				pdf.Integer(253),
				pdf.Integer(473),
				pdf.Integer(337),
			},
			pdf.Name("Contents"): pdf.String("New Text #3\\203a longer text annotation which we will continue \\\nonto a second line"),
			pdf.Name("Open"):     pdf.Boolean(true),
		},
	})
	if err != nil {
		log.Fatal(errgo.Details(err))
	}
	annotations = append(annotations, ref)

	// update the annotations array
	minimal.Add(pdf.IndirectObject{
		ObjectReference: pdf.ObjectReference{ObjectNumber: 7},
		Object:          annotations,
	})

	err = minimal.Save()
	if err != nil {
		log.Fatalln(errgo.Details(err))
	}
}
Exemple #12
0
// create the minimal file described in H.2
func createMinimalFile() {
	log.Printf("createMinimalFile")

	minimal, err := pdf.Create("h7-minimal.pdf")
	if err != nil {
		log.Fatalln(errgo.Details(err))
	}
	defer minimal.Close()

	minimal.Root = pdf.ObjectReference{ObjectNumber: 1}

	// catalog
	minimal.Add(pdf.IndirectObject{
		ObjectReference: pdf.ObjectReference{ObjectNumber: 1},
		Object: pdf.Dictionary{
			pdf.Name("Type"): pdf.Name("Catalog"),
			pdf.Name("Outlines"): pdf.ObjectReference{
				ObjectNumber: 2,
			},
			pdf.Name("Pages"): pdf.ObjectReference{
				ObjectNumber: 3,
			},
		},
	})

	// outlines
	minimal.Add(pdf.IndirectObject{
		ObjectReference: pdf.ObjectReference{ObjectNumber: 2},
		Object: pdf.Dictionary{
			pdf.Name("Type"):  pdf.Name("Outlines"),
			pdf.Name("Count"): pdf.Integer(0),
		},
	})

	// pages
	minimal.Add(pdf.IndirectObject{
		ObjectReference: pdf.ObjectReference{ObjectNumber: 3},
		Object: pdf.Dictionary{
			pdf.Name("Type"): pdf.Name("Pages"),
			pdf.Name("Kids"): pdf.Array{
				pdf.ObjectReference{
					ObjectNumber: 4,
				},
			},
			pdf.Name("Count"): pdf.Integer(1),
		},
	})

	// page
	minimal.Add(pdf.IndirectObject{
		ObjectReference: pdf.ObjectReference{ObjectNumber: 4},
		Object: pdf.Dictionary{
			pdf.Name("Type"): pdf.Name("Page"),
			pdf.Name("Parent"): pdf.ObjectReference{
				ObjectNumber: 3,
			},
			pdf.Name("MediaBox"): pdf.Array{
				pdf.Integer(0),
				pdf.Integer(0),
				pdf.Integer(612),
				pdf.Integer(792),
			},
			pdf.Name("Contents"): pdf.ObjectReference{
				ObjectNumber: 5,
			},
			pdf.Name("Resources"): pdf.Dictionary{
				pdf.Name("ProcSet"): pdf.ObjectReference{
					ObjectNumber: 6,
				},
			},
		},
	})

	// content stream
	minimal.Add(pdf.IndirectObject{
		ObjectReference: pdf.ObjectReference{ObjectNumber: 5},
		Object: pdf.Stream{
			Dictionary: pdf.Dictionary{
				pdf.Name("Length"): pdf.Integer(0),
			},
		},
	})

	// procset
	minimal.Add(pdf.IndirectObject{
		ObjectReference: pdf.ObjectReference{ObjectNumber: 6},
		Object: pdf.Array{
			pdf.Name("PDF"),
		},
	})

	minimal.Root = pdf.ObjectReference{ObjectNumber: 1}

	err = minimal.Save()
	if err != nil {
		log.Fatalln(errgo.Details(err))
	}
}
Exemple #13
0
// Stage 1: Add Four Text Annotations
func stage1() {
	log.Println("stage 1")

	minimal, err := pdf.Open("h7-minimal.pdf")
	if err != nil {
		log.Fatalln(errgo.Details(err))
	}

	// page
	page := minimal.Get(pdf.ObjectReference{ObjectNumber: 4}).(pdf.Dictionary)
	page[pdf.Name("Annots")] = pdf.ObjectReference{ObjectNumber: 7}
	minimal.Add(pdf.IndirectObject{
		ObjectReference: pdf.ObjectReference{ObjectNumber: 4},
		Object:          page,
	})

	// annotation array
	minimal.Add(pdf.IndirectObject{
		ObjectReference: pdf.ObjectReference{ObjectNumber: 7},
		Object: pdf.Array{
			pdf.ObjectReference{ObjectNumber: 8},
			pdf.ObjectReference{ObjectNumber: 9},
			pdf.ObjectReference{ObjectNumber: 10},
			pdf.ObjectReference{ObjectNumber: 11},
		},
	})

	// annotation
	minimal.Add(pdf.IndirectObject{
		ObjectReference: pdf.ObjectReference{ObjectNumber: 8},
		Object: pdf.Dictionary{
			pdf.Name("Type"):    pdf.Name("Annot"),
			pdf.Name("Subtype"): pdf.Name("Text"),
			pdf.Name("Rect"): pdf.Array{
				pdf.Integer(44),
				pdf.Integer(616),
				pdf.Integer(162),
				pdf.Integer(735),
			},
			pdf.Name("Contents"): pdf.String("Text #1"),
			pdf.Name("Open"):     pdf.Boolean(true),
		},
	})

	// annotation
	minimal.Add(pdf.IndirectObject{
		ObjectReference: pdf.ObjectReference{ObjectNumber: 9},
		Object: pdf.Dictionary{
			pdf.Name("Type"):    pdf.Name("Annot"),
			pdf.Name("Subtype"): pdf.Name("Text"),
			pdf.Name("Rect"): pdf.Array{
				pdf.Integer(224),
				pdf.Integer(668),
				pdf.Integer(457),
				pdf.Integer(735),
			},
			pdf.Name("Contents"): pdf.String("Text #2"),
			pdf.Name("Open"):     pdf.Boolean(false),
		},
	})

	// annotation
	minimal.Add(pdf.IndirectObject{
		ObjectReference: pdf.ObjectReference{ObjectNumber: 10},
		Object: pdf.Dictionary{
			pdf.Name("Type"):    pdf.Name("Annot"),
			pdf.Name("Subtype"): pdf.Name("Text"),
			pdf.Name("Rect"): pdf.Array{
				pdf.Integer(239),
				pdf.Integer(393),
				pdf.Integer(328),
				pdf.Integer(622),
			},
			pdf.Name("Contents"): pdf.String("Text #3"),
			pdf.Name("Open"):     pdf.Boolean(true),
		},
	})

	// annotation
	minimal.Add(pdf.IndirectObject{
		ObjectReference: pdf.ObjectReference{ObjectNumber: 11},
		Object: pdf.Dictionary{
			pdf.Name("Type"):    pdf.Name("Annot"),
			pdf.Name("Subtype"): pdf.Name("Text"),
			pdf.Name("Rect"): pdf.Array{
				pdf.Integer(34),
				pdf.Integer(398),
				pdf.Integer(225),
				pdf.Integer(575),
			},
			pdf.Name("Contents"): pdf.String("Text #4"),
			pdf.Name("Open"):     pdf.Boolean(false),
		},
	})

	err = minimal.Save()
	if err != nil {
		log.Fatalln(errgo.Details(err))
	}
}